gpt4 book ai didi

javascript - 类型错误: Cannot read property 'from' of undefined at admin. firestore.collection.doc.collection.doc.get.then.queryResult

转载 作者:行者123 更新时间:2023-12-03 00:14:48 24 4
gpt4 key购买 nike

所以我在这里失去了理智:/我正在尝试使用 Firebase 的功能在用户之间发送通知。

该函数看起来没问题,并成功获取路径(在日志中检查过),但在尝试访问文档时,即使文档存在,仍返回“无文档”。我遇到了很多类似的问题,但没有找到解决方案,请帮助:)

这是函数:

'use-strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.firestore.document("users/{user_id}/notifications/{notification_id}").onWrite((change,context) =>{

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log("User ID: " + user_id + " | Notification ID: " + notification_id);

return admin.firestore().collection("users").doc(user_id).collection("notifications").doc("notification_id").get().then(queryResult =>{
if (!queryResult.exists) {
console.log('No documents');
return null;

}else{
const from_user_id = queryResult.data().from;

const from_data = admin.firestore().collection("users").doc(from_user_id).get();
const to_data = admin.firestore().collection("users").doc(user_id).get();

return Promise.all([from_data, to_data]).then(result =>{

const from_name = reuslt[0].data().name;
const to_name = reuslt[1].data().name;

console.log("FROM: " + from_name + " TO: " + to_name);
return null;
});
}
}).catch(err =>{ console.error('Error getting document', err); }); });

这是来自 firebase 的:

1:00:23.767 AM sendNotification Function execution took 189 ms, finished with status: 'error'

1:00:23.762 AMsendNotification TypeError: Cannot read property 'from' of undefined at admin.firestore.collection.doc.collection.doc.get.then.queryResult (/user_code/index.js:15:44) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

1:00:23.585 AM sendNotification User ID: mpIfC6Ca41eBdwNzwwX3GJaHIfb2 | Notification ID: v89cRRcV2np8IsAwbRu0

1:00:23.578 AM sendNotification Function execution started

最佳答案

在路径中写入 ("notification_id"),而不是我创建的 const 中的 (notification_id)。

只用了 2 个小时就搞清楚了:|

关于javascript - 类型错误: Cannot read property 'from' of undefined at admin. firestore.collection.doc.collection.doc.get.then.queryResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com