附表的云功能 [英] Cloud Function with Schedule

查看:45
本文介绍了附表的云功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过添加时间表来更改Firebase内部节点状态的计划来实现云功能,即将{active:true}更改为{active:false}

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)});          

});

这是我考虑过的代码,我在哪里传递了从firebase获取的参数以激活计划,但是在通过onCreate激活函数时,firebase会向我返回此错误.

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.

  • 函数返回了未定义的预期承诺或值

推荐答案

您要尝试执行的操作是不可能的.您不能在另一个函数声明中声明和导出函数.如果您希望按计划运行某些内容,则必须在文件的顶层声明并导出它.

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.

您可以做的是让您的函数按计划运行,但是查询Firestore以查看其在每次调用时应如何运行.

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.

这篇关于附表的云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆