简体   繁体   English

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

[英]How to update a Firestore field from a pub sub function

I have set up a pub/sub function to fire every minute and I want to check if the current time is equal to the start time any of the Firestore entities then change a value if it is :我已经设置了一个 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'})));
};

The .get() method returns a Promise<QuerySnapshot> , and not a QuerySnapshot . .get()方法返回一个Promise<QuerySnapshot> ,而不是一个QuerySnapshot You will need to await the snapshot, or use a then() block.您将需要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'})));
};

or或者

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM