Node.js 获取文档不是一个函数
我试图检索存储为Node.js 获取文档不是一个函数,node.js,google-cloud-functions,Node.js,Google Cloud Functions,我试图检索存储为Firestore文档的对象的boolean子对象(notificationsOn),以查看是否应执行函数的其余部分 如果语句出现“threadDoc.get不是函数”错误,则将let threadDoc中的部分添加到if语句中即可完成整个函数。我认为我的语法是错误的,但我不知道怎么做,因为类似的函数在函数的后面部分工作: const functions = require('firebase-functions'); const admin = require('firebas
Firestore
文档的对象的boolean
子对象(notificationsOn),以查看是否应执行函数的其余部分
如果语句出现“threadDoc.get不是函数”错误,则将let threadDoc
中的部分添加到if
语句中即可完成整个函数。我认为我的语法是错误的,但我不知道怎么做,因为类似的函数在函数的后面部分工作:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendDMNotification =functions.firestore.document('/dm_threads/{thread_id}/messages/{message_id}').onCreate((snapshot, context) => {
const newMessage = snapshot.data();
const senderName = newMessage.authorName;
const senderID = newMessage.authorUID;
const messageText = newMessage.message;
const recipientID = newMessage.recipientUID;
var notificationsOn = null;
let deviceTokenQuery = admin.firestore().collection(`/users/${recipientID}/device_tokens/`);
var idsToBeSorted = [senderID, recipientID];
idsToBeSorted.sort();
var threadID = idsToBeSorted[0] + idsToBeSorted[1];
console.log(recipientID);
console.log(threadID);
let threadDoc = admin.firestore().document(`users/${recipientID}/threads/${threadID}/`);
return threadDoc.get().then(doc => {
let notificationsOn = doc.data.notificationsOn;
console.log(notificationsOn);
if (notificationsOn !== false){
return deviceTokenQuery.get().then(querySnapshot => {
let tokenShapshot = querySnapshot.docs;
const notificationPromises = tokenShapshot.map(doc => {
let token_id = doc.data().tokenID;
const payload = {
data: {
title: senderName,
body: messageText,
senderID: senderID,
senderName: senderName
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log("Notification sent: ", response);
})
.catch(error => {
console.log("Error sending message: ", error);
});
});
return Promise.all(notificationPromises);
});
}
return;
});
});
我想你的意思是说admin.firestore()
而不是functions.firestore
admin.firestore().document()
应该是admin.firestore().collection(…).doc(…)
这解决了我的问题不确定您指的是第6行还是第29行,但如果切换它们,我会得到相同的错误。第6行的functions.firestore一直在工作,第一个日志语句之后的检查是破坏一切的原因。functions
对象只帮助您构建云函数。就这些<代码>管理
为您提供对Firebase产品的编程访问。我明白了。正如上面所写,我在初始化threadDoc时将其替换为admin.firestore().document,但在控制台中仍然会出现相同的错误。前两个日志语句打印出来。这就是答案,解决了我的问题。我编辑它是为了说明这一点。嗨@aedgar777!找到解决方案干得好!你能?它将使它更为明显,并在您找到解决方案时帮助有相同问题的人。谢谢计划好了。这让我又等了8个小时。