【发布时间】:2021-08-18 08:28:46
【问题描述】:
我在编写计划函数以从 Firestore 读取数据时遇到问题。该函数每 1 分钟成功运行一次,但现在问题是从 firestore 读取数据。我正在使用 async-await 因为我想在读取后遍历数据然后执行一些更新。请帮助我第一次使用 firebase 功能。 下面是我的功能。我不断收到此错误无法读取未定义的属性映射
exports.checkDefaultedPledges = functions.pubsub.schedule("every 1 minutes").onRun( async
(context) => {
console.log("This will be run every 2 minutes!");
const time = new Date().getTime();
const snapshot = db.collection("pledges").get();
const res = await snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
console.log(res);
return null;
});
我是否可以在不使用 .then() 的情况下编写此函数?
【问题讨论】:
-
"我是否可以在不使用 .then() 的情况下编写此函数?" => 是的。您到底想更新什么(哪些文档和哪些数据?快照中的所有文档?等等)?
-
@RenaudTarnec 在更新之前。我遇到的问题是读取数据时没有返回数据。我收到此错误无法读取未定义的属性映射。
-
你需要做
const snapshot = await db.collection("pledges").get();,因为get()是异步的。而且你不应该在const res = await snapshot.docs.map()中使用await,因为 maps 是一个简单的属性(这里没有异步性)。 -
以上评论中的错字。应该阅读:“因为
docs是一个简单的属性(这里没有异步性)” -
谢谢@RenaudTarnec。有效。我现在明白发生了什么
标签: javascript node.js firebase google-cloud-firestore google-cloud-functions