用于循环异步的Firebase函数正在等待

西蒙

从理论上讲,我正在尝试使用Firebase Functions做一个相对简单的函数。

特别:

  • + 1为所有用户添加到实时数据库变量

  • 向所有用户发送通知

我仍在尝试了解async/await这可能就是为什么我为此付出很多努力的原因。

这是我在做什么:

 exports.gcIncrement = functions.database
  .ref('gthreads/{threadId}/messages/{messageId}')
  .onCreate(async (snapshot, context) => {

    const data = snapshot.val();
    const threadId = context.params.threadId;
    const uid = context.auth.uid;
    
    adb.ref('gchats/' + threadId).once('value').then(async (gchatData) => {
    const parent = gchatData.val();
    incrementUser(parent.users, uid, threadId); //parent.users is an object with 1-30 users.
    sendGCNotification(parent.users, data);
    return true;
  }).catch(error => console.log(error))
});

然后我有功能incrementUser

function IncrementUser(array, uid, threadId) {
    for (const key in array) {
      if (key != uid) {
        const gcMessageRef =
        adb.ref('users/' + key + '/gthreads/' + threadId + '/' + threadId+'/unread/');
        gcMessageRef.transaction((int) => {
          return (int || 0) + 1;
      }
    }
  }

和功能sendGCNotification

  function sendGCNotification(array, numbOfMsg, data) {
    let payload = {
      notification: {
        title: 'My App - ' + data.title,
        body: "This is a new notification!",
      }
    }
    const db = admin.firestore()
    for (const key in array) {
      if (!data.adminMessage) {
        if (array[key] === 0) {

          const devicesRef = db.collection('devices').where('userId', '==', key)

          const devices = await devicesRef.get();
          devices.forEach(result => {
            const tokens = [];
            const token = result.data().token;
            tokens.push(token)

            return admin.messaging().sendToDevice(tokens, payload)
          })

        }
      }
    }
  }
    

我目前收到错误:

仅在异步函数中允许使用“ await”表达式。

const devices =等待devicesRef.get();

但是即使我没有错误,它似乎也不起作用。Firebase功能日志显示:

4:45:26.207 PM gcIncrement函数执行耗时444毫秒,状态为:“ ok” 4:45:25.763 PM gcIncrement函数执行开始

因此,它似乎按预期运行,但未按预期执行代码。有任何想法吗?谢谢!

道格·史蒂文森

的所有使用await都必须在标记为的函数的主体内进行async您的功能sendGCNotification不是异步的。您必须将其标记为异步,并确保已等待其中的所有promise,或者返回在完成所有异步工作后解析的promise。

另外,IncrementUser您不处理gcMessageRef.transaction()返回的promise。您需要处理从所有异步工作中生成的每个promise,并确保它们都是您从顶级函数返回或等待的最终promise的一部分。

如果您想在Cloud Functions代码中了解有关Promise和async / await的更多信息,建议您使用我的视频系列具体来说,标题为“异步/等待如何与TypeScript和ECMAScript 2017一起使用?”。即使您没有使用TypeScript,异步/等待也以相同的方式工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章