Cloud Function with Schedule

為{幸葍}努か 提交于 2020-01-21 19:20:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!