我正在尝试用我的移动应用程序做一个简单的聊天应用程序firebase功能。这是我当前的javascript代码:
'use-strict'
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
exports.sendCommentNotification=functions.firestore.document("Notifications/{user_id}/Comment/{comment_id}").onWrite((change,context)=> {
const user_id=context.params.user_id;
const comment_id=context.params.comment_id;
console.log(user_id+":"+comment_id);
return admin.firestore().collection("Notifications").doc(user_id).collection("Comment").doc(comment_id).get().then((queryResult)=>{
const post_id=queryResult.data().post_id;
const admin_user_id=queryResult.data().admin_id;
const noti_id=queryResult.data().notification_id;
const timestamp=queryResult.data().timestamp;
const post_desc=queryResult.data().post_desc;
const admin_data=admin.firestore().collection("Users").doc(admin_user_id).get();
const commenter_data=admin.firestore().collection("Users").doc(user_id).get();
return Promise.all([commenter_data,admin_data]).then(result=>{
const commenter_name=result[0].data().name;
const commenter_image=result[0].data().image;
const admin_token=result[1].data().token_id;
const admin_name=result[1].data().name;
我的Firebase日志
TypeError: Cannot read property 'post_id' of undefined
at admin.firestore.collection.doc.collection.doc.get.then (/user_code/index.js:20:41)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
答案 0 :(得分:0)
queryResult.data()
返回undefined
,这意味着您查询的文档不存在。在尝试访问其数据之前,应检查布尔值queryResult.exists
以查看文档是否确实存在。有关更多详细信息,请参见DocumentSnapshot的文档-这就是queryResult
。