【发布时间】:2021-07-21 01:31:22
【问题描述】:
创建另一个文档时,我需要从 Firestore 文档中检索信息。当我尝试这样做时,我遇到了关于函数不是异步的错误。我已经很久没有使用javascript了,我基本上又是一个新手,不知道如何解决这个问题。
好的,所以我正在使用 Firebase Cloud Functions,而有问题的函数是 Firestore .onCreate() 触发器。
当函数被触发时,我设置了一个 sender 变量(这是我需要检索的不同集合中的文档 ID)
然后我尝试按照文档获取文档。
函数最终是这样的:
exports.pushFriendRequestNotification = functions.firestore.document('friends/{friendID}')
.onCreate((snap, context) => {
// when friend request is created
data = doc.data()//get request data
sender = data["sender"]//get request sender from data
const requestRef = db.collection('User').doc(sender);
const doc = await requestRef.get();//get user data of sender
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
});
当我在模拟器中运行它时,我得到了这个错误:
const doc = await requestRef.get();//get user data of sender
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
我完全不知道从这里去哪里。
谁能帮我解决这个问题? 谢谢
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions