我创建了一个 Firebase 函数来触发推送通知,但我在 Firebase 上得到以下寄存器:“函数返回未定义,预期的 Promise 或值”和“snapshot.docs 在 admin.firestore.collection.get.then 上不可迭代” (/srv/index.js:20:38)“。你们有什么想法如何修复它吗?
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)
最佳答案
您不会从顶级函数定义中返回任何值。这意味着 Google Cloud Functions 环境无法知道您的函数何时完成,因此也无法知道何时停止向您收取其使用费用。
为了确保 Cloud Functions 知道您的代码何时完成,您需要从顶级函数返回一个值(在所有工作同步发生的情况下),或者从顶级函数返回一个 promise (如果函数关闭 }
后某些工作仍在继续)。
现在,您的云函数中的顶级代码没有返回任何内容,这意味着调用此函数的结果是未定义
。这就是 Cloud Functions 环境所提示的。
在您的例子中,您正在从 Firestore 加载数据,这是一个异步操作,因此您需要返回一个 promise :
exports.destaquesTrigger = functions.firestore.document('destaques/{destaquesId}').onCreate((snapshot, context) => {
msgData = snapshot.data();
return admin.firestore().collection('pushtokens').get().then((snapshots) =>
{
var tokens = [];
if (snapshots.empty) {
console.log('No Devices');
return false;
} 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);
})
}
})
})
我所做的改变:
添加了返回
return admin.firestore().collection('pushtokens').get()
。这是必需的,以便您的return admin.messaging().sendToDevice(
可以“冒泡”返回到 Cloud Functions。添加了
return false
以防没有快照,否则您不会返回任何内容。
关于javascript - 为 Firebase 创建函数时如何修复 "Function returned undefined, expected Promise or value"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56501586/