【发布时间】:2019-02-04 12:27:59
【问题描述】:
我使用以下查询部署了一个函数:
admin.firestore().collection("fcm").where("devices",'array-contains', mobile).get().then((snapshots)=> {...});
这会从 Cloud Function Log 中返回以下错误:
msgTrigger: Function execution started
msgTrigger: Function returned undefined, expected Promise or value
msgTrigger: Function execution took 8429 ms, finished with status: 'ok'
msgTrigger: Unhandled rejection
msgTrigger: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined at admin.firestore.collection.where.get.then (/user_code/index.js:23:65) at process._tickDomainCallback (internal/process/next_tick.js:135:7)
有人吗?
在这里与编辑战斗了好几天。决定分块发布我的功能代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var msgData;
var mobile;
第二部分:
exports.msgTrigger = functions.firestore
.document('Messages/{MessageID}')
.onCreate((snapshot, context) => {
msgData = snapshot.data();
mobile = msgData.mobile;
admin.firestore().collection("fcm").where("devices", 'array-contains', mobile).get().then((snapshots) => {
第三部分:
var tokens = [];
if (snapshots.empty) {
console.log('No devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().token);
}
var payLoad = {
"notification": {
"title": "de " + msgData.name,
"body": "Alerta de Emergência!",
"sound": "default",
"icon": msgData.icon
},
"data": {
"remetente": +msgData.name,
"mensagem": "Alerta de Emergência!"
}
}
第四部分:
return admin.messaging().sendToDevice(tokens, payLoad).then((response) => {
console.log("mensagens enviadas");
}).catch((err) => {
console.log("erro: " + err);
});
}
});
});
【问题讨论】:
-
您能分享一下您的云函数的整个代码吗?还有您使用的是哪个版本的 Firestore 和 Cloud Functions。
-
很难通过编辑添加功能代码。一直说看起来我的帖子主要有代码,请添加一些额外的信息。但是,添加其他信息不会使错误消失。
-
@Renaud。我将功能分成几个部分,以便能够在此处发布(只是无法在 1 中将其发布到此处)。我正在使用 Firestore ^0.8.2+1, "firebase-admin": "~6.0.0", "firebase-functions": "^2.1.0"
标签: javascript firebase google-cloud-firestore google-cloud-functions