node.js - 在函数日志中使用 firebase 处理通知时出现错误,我该如何解决?
问题描述
我正在使用颤振构建一个移动应用程序,我想让它支持自动推送通知,然后我使用了 firebase 云消息传递。我写的函数:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//admin.initializeApp();
var msgData;
exports.offerTrigger = functions.firestore.document('requests/{requestId}'
).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 snapshots.docs){
tokens.push(token.data().deviceId);
}
var payload = {
"notification" : {
"title" : "From" + msgData.Name,
"body" : "Request " + msgData.requestDetial,
"sound" : "default"
},
"data" : {
"SenderName" : msgData.Name,
"message" : msgData.requestDetial
}
}
admin.messaging().sendToDevice(tokens,payload).then((respone) => {
console.log("pushed them all");
}).catch((err) => {
console.log(err);
});
}
})
})
在我部署了我的函数之后,当我添加一些文档时,我在 firebase 函数中遇到了一个错误,记录了错误:
TypeError: admin.firestore(...).Collection 不是函数
在exports.offerTrigger.functions.firestore.document.onCreate (/user_code/index.js:9:23)
在 cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:114:23)
在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:144:20)
在 /var/tmp/worker/worker.js:827:24
在 process._tickDomainCallback (internal/process/next_tick.js:135:7)
我该如何解决这个错误?
解决方案
改变这个:
admin.firestore().Collection('pushtokens').get().then((snapshots) => {
进入这个:
admin.firestore().collection('pushtokens').get().then((snapshots) => {
来自文档:
获取一个
CollectionReference
引用指定路径上的集合的实例。
推荐阅读
- javascript - 如何从javascript中的电话字符串中删除一组字符?
- grpc - grpc 连接管理的连接数不匹配
- javascript - JavaScript .find 在 Angular Observable 服务中返回未定义
- angular - 错误:调用 saveChanges 时发生未知错误。在 JSDO 中
- python - 使用 Pyinstaller 构建的 Python exe 无法在所有机器上运行,因为它没有必要的 MS 证书
- javascript - 如何正确升级 React Native?
- java - 使用 Java WatchService 监控和解析日志文件
- node.js - AWS Lambda 上的 Node、Postgres 出现“连接意外终止”错误
- python - 使用 CORS 提供静态文件夹的 Flask
- telegram - 是否可以在 Telegram 中提供“支持”联系人/组?(一个只写组或一个机器人)