问题
I am trying to do a cloud function by adding a Schedule to change the status of a node inside firebase, which is {active: true} to {active: false}
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.createPrd = functions.firestore.document('products/{itemId}').onCreate((ev) => {
db.collection('products').doc(ev.id).get().then( doc => {
var dia = doc.data().limitDay;
var mes = doc.data().limitMonth;
var hora = doc.data().limitHour;
exports.schedulerFun = functions.pubsub.schedule(dia+" "+mes+" "+hora).onRun((context) => {
console.log('Se acabo la buena onda', doc.data().productName)
db.collection('products').doc(ev.id).update({
activo: false
})
})
return 0;
}).catch(err => {console.log(err)});
});
It is the code I had thought about where I passed parameters that I took from firebase to activate the schedule, but firebase returns this error to me when activating the function through onCreate.
- Function returned undefined, expected Promise or value
回答1:
What you're trying to do isn't possible. You can't declare and export a function inside another function declaration. If you want something to run on a schedule, you will have to declare and export it at the top level of the file.
What you can do instead is have your function run on a schedule, but query Firestore to see how it should operate on each invocation.
来源:https://stackoverflow.com/questions/58823930/cloud-function-with-schedule