本文介绍了如何解决“函数返回的未定义,期望的承诺或价值"?为Firebase创建功能时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Firebase函数来触发推送通知,但是我在Firebase上获得了以下寄存器,函数返回了未定义的,预期的承诺或值"和"snapshot.docs"在admin.firestore.collection.get.then不可迭代(/srv/index.js:20:38).你们对如何解决有任何想法吗?

I created a Firebase function to trigger push notifications, but I get the following register on Firebase,"Function returned undefined, expected Promise or value" and "snapshot.docs is not iterable at admin.firestore.collection.get.then(/srv/index.js:20:38)".Do you guys have any Idea how to fix it?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

var msgData;


exports.destaquesTrigger = functions.firestore.document(
'destaques/{destaquesId}'
).onCreate((snapshot, context) => {
msgData = snapshot.data();

admin.firestore().collection('pushtokens').get().then((snapshots) => 
{
   var tokens = [];
   if (snapshots.empty) {
        console.log('No Devices');

     } else {
      for (var token of snapshot.docs) {
          tokens.push(token.data().devtoken);
      }

      var payload = {
         "notification": {
             "title": msgData.notif1,
             "body": msgData.notif2,
             "sound": "default"
          },
          "data": {
             "sendername": msgData.notif3,
             "message": msgData.notif4,
         }
       }

       return admin.messaging().sendToDevice(tokens, 
 payload).then((response) => {
          console.log('Pushed them all');
       }).catch((err) => {
           console.log(err);
       })
      }

    })
  })

错误:

6:22:54.175 PM destaquesTrigger Function execution started
6:22:54.175 PM destaquesTrigger Billing account not configured. 
External network is not accessible and quotas are severely 
limited. Configure billing account to remove these restrictions
6:22:54.676 PM destaquesTrigger Function returned undefined, 
expected Promise or value
6:22:57.168 PM destaquesTrigger Function execution took 2993 ms, 
finished with status: 'ok'
6:23:23.652 PM destaquesTrigger Unhandled rejection
6:23:23.752 PM destaquesTrigger TypeError: snapshot.docs is not 
iterable at admin.firestore.collection.get.then
(/srv/index.js:20:38) at <anonymous> at 
process._tickDomainCallback (internal/process/next_tick.js:229:7)

推荐答案

我认为Kanitz先生已经解决了他的问题.但是,由于没有人回答他问题的后半部分,因此我可能有一个解决方案.更改以下代码段:

I am assuming that Mr. Kanitz has solved his issue. But since no one answered the latter part of his question, I may have a possible solution. Change the following snippet:

(var token of snapshot.docs)

收件人:

(var token of snapshots.docs)

我认为您使用了错误的变量来获取文档!希望对Raja Yogan的FCM教程有所帮助的人!

I think you used the wrong variable to get docs from! Hope that helps anyone else following Raja Yogan's FCM tutorial!

这篇关于如何解决“函数返回的未定义,期望的承诺或价值"?为Firebase创建功能时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:10