我设置了一个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()块。

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'})));
  })
};

09-26 01:44