【问题标题】:How to create a scheduled function that reads data from Firebase and then sends a message on twilio如何创建从 Firebase 读取数据然后在 twilio 上发送消息的计划函数
【发布时间】:2020-08-18 08:24:19
【问题描述】:

我正在尝试从我的数据库中读取数据,然后通过 Twilio 发送消息。 我期望这个函数做的是每分钟检查一次数据库。它将检查玩家集合中的所有文档。如果其中一个文档中的会话字段等于 0,则它会创建一条消息并将其发送到同一文档中的电话号码。

这是我当前的代码:

const account = '***********************************';
const auth = '********************************';
const client = require('twilio')(account,auth);
const firebaseFunc = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const database = admin.firestore()

exports.paymentChecker = firebaseFunc.pubsub.schedule('* * * * *').onRun((context) => {
    database.collection('Players').get()
    .then(querySnapshot => {
        querySnapshot.forEach(doc => {
            if(doc.data().sessions, '==', 0){
                client.messages.create({
                    to: doc.data().phone,
                    from: '+***********',
                    body: 'Hi' + doc.data().First + ', you have to renew your registration',
                })
            }
        })
    }).catch()
})

但是,此代码不会部署到 firebase,因为它会产生许多错误。

19:5   error  Expected catch() or return                  promise/catch-or-return
  20:11  error  Each then() should return a value or throw  promise/always-return
  22:16  error  Unexpected constant condition               no-constant-condition

如何解决这个问题? 谢谢

【问题讨论】:

  • 我建议对这些错误中的每一个进行谷歌搜索,以了解它们的含义。一旦你明白他们在说什么,你就可以采取措施纠正任何可能的错误。

标签: javascript firebase google-cloud-functions twilio


【解决方案1】:

问题在于您没有返回异步方法(get()client.messages.create())返回的 Promise。请参阅doc,了解有关它的重要性以及它生成的代码错误的更多详细信息。

所以以下应该可以解决问题(未经测试):

exports.paymentChecker = firebaseFunc.pubsub.schedule('* * * * *').onRun((context) => {
    return database.collection('Players').get()
        .then(querySnapshot => {
            const promises = [];
            querySnapshot.forEach(doc => {
                if (doc.data().sessions == 0) {
                    promises.push(
                        client.messages.create({
                            to: doc.data().phone,
                            from: '+***********',
                            body: 'Hi' + doc.data().First + ', you have to renew your registration',
                        })
                    );
                }
            })
            return Promise.all(promises);
        }).catch(error => {
            console.log(error);
            return null;
        })
});

还要注意if(doc.data().sessions, '==', 0) 不能工作。与其他错误一样,您可以通过注意相应错误行开头提到的行号来检测它:22:16 error Unexpected constant condition no-constant-condition)。

【讨论】:

  • @kerimErkan 嗨,您有时间查看建议的答案吗?谢谢
【解决方案2】:

通过查看您分享的错误,它似乎与 Promise return 有关。承诺只是一项可能尚未完成的任务。您可以查看这篇博文 [1],它通过示例解释了 Promise。我希望这将有助于解决这个问题。

[1]https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2021-11-20
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多