【问题标题】:Can anyone tell me why it is showing array is empty?谁能告诉我为什么它显示数组是空的?
【发布时间】:2021-03-03 12:25:39
【问题描述】:

我想向多个设备发送通知,为此我通过查询文档获取令牌并将令牌保存到数组中,但它显示数组为空。最可能的错误是因为我无法在数组中添加元素。

我的代码是:-

var registrationTokens=[];

                const indexOfSender=usersList.indexOf(chatItem.senderUsername);
                let removedUsername=usersList.splice(indexOfSender,1);      //to remove the senders name from list

                usersList.forEach(async(element)=>{
                    const query = admin.firestore().collection('users').where("username","==",element);
                    const querySnapshot = await query.get();
                    if (querySnapshot.docs.length > 0) {
                        const doc = querySnapshot.docs[0];
                        const data = doc.data();
                        registrationTokens.push(data.androidNotificationToken); //adding token over here
                    }
                    else {
                        console.log("Unable to get token for the username ", element);
                    }
                });
                
                
                const message =
                {
                    notification: {
                                    title:'Message',
                                    body: body,
                                    imageUrl: url,
                                  },
                    tokens: registrationTokens,
                    data: { recipient: senderUserId },
                };

                admin.messaging().sendMulticast(message)
                .then(response =>
                {
                if (response.failureCount > 0) {
                      const failedTokens = [];
                      response.responses.forEach((resp, idx) => {
                        if (!resp.success) {
                          failedTokens.push(registrationTokens[idx]);
                        }
                      });
                      console.log('List of tokens that caused failures: ' + failedTokens);
                    }
                else
                {
                    console.log('Successfully sent messages ', response);
                }
              });

错误

Error: tokens must be a non-empty array
at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:254:16)
at Messaging.sendMulticast (/workspace/node_modules/firebase-admin/lib/messaging/messaging.js:294:19)
at sendNotificationForGroupChat (/workspace/index.js:238:35)
at exports.onCreateMessage.functions.region.firestore.document.onCreate (/workspace/index.js:116:9)
at process._tickCallback (internal/process/next_tick.js:68:7) 

【问题讨论】:

    标签: javascript node.js google-cloud-firestore google-cloud-functions firebase-cloud-messaging


    【解决方案1】:

    for-of 循环可以很好地处理异步调用:)

    干杯

    【讨论】:

      【解决方案2】:

      async 内的forEach 无法按您预期的方式工作。如果添加一些日志记录,您将看到循环在其任何异步工作完成之前结束,在将令牌数组传递给 FCM 之前将其留空。循环中的每次迭代都只会生成一个未解决的 Promise。在调用 FCM 之前,您需要重写代码以实际等待所有这些承诺。

      了解更多信息:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        • 2020-02-16
        • 2021-04-16
        相关资源
        最近更新 更多