gpt4 book ai didi

javascript - 如何从 pub sub 函数更新 Firestore 字段

转载 作者:行者123 更新时间:2023-12-02 22:03:17 25 4
gpt4 key购买 nike

我已经设置了一个每分钟触发一次的 pub/sub 函数,我想检查当前时间是否等于任何 Firestore 实体的开始时间,然后更改一个值(如果是):

exports.updateDraftStatus = (event, context) => {
const pubsubMessage = event.data;
console.log(Buffer.from(pubsubMessage, 'base64').toString());

const db = admin.firestore();
time = admin.firestore.Timestamp.fromMillis(Date.now())
const snap = db.collection("leagues").where("startTime", time).get()
await Promise.all(snap.docs.map(doc => doc.ref.update({Started: 'Off'})));
};

最佳答案

.get() method返回 Promise<QuerySnapshot> ,而不是 QuerySnapshot 。您需要await快照,或使用then() block 。

exports.updateDraftStatus = async (event, context) => {
const pubsubMessage = event.data;
console.log(Buffer.from(pubsubMessage, 'base64').toString());

const db = admin.firestore();
time = admin.firestore.Timestamp.fromMillis(Date.now())
const snap = await db.collection("leagues").where("startTime", time).get()
await Promise.all(snap.docs.map(doc => doc.ref.update({Started: 'Off'})));
};

exports.updateDraftStatus = (event, context) => {
const pubsubMessage = event.data;
console.log(Buffer.from(pubsubMessage, 'base64').toString());

const db = admin.firestore();
time = admin.firestore.Timestamp.fromMillis(Date.now())
return db.collection("leagues").where("startTime", time).get().then((snap) => {
return Promise.all(snap.docs.map(doc => doc.ref.update({Started: 'Off'})));
})
};

关于javascript - 如何从 pub sub 函数更新 Firestore 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804396/

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