返回的功能未定义和承诺在功能中未定义firebase

发布于 2025-01-23 22:27:08 字数 704 浏览 0 评论 0原文

这是我的第一个firebase功能,它给了我很多头号:)目的是每24小时从收集用户那里更新所有文档的计数字段。

    async function clearCountField() {
  console.log("Clearing count task start point.");
    const userSnapshots = db.collection('users').where('count', '!=', '').where('nou', '==', false).get().then(snapshot => {
      const promises = [];
      snapshot.forEach(doc => {
        promises.push(doc.ref.update({ 'count': '' }));
      });
    });
    return Promise.all(promises)
}



export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
   const promise = clearCountField().then(resolve,reject); 
});

我做错了什么?

This is my first firebase function and it gives me a lot of headic :) The purpose is to update the count field from all documents from collection users every 24 hours.

    async function clearCountField() {
  console.log("Clearing count task start point.");
    const userSnapshots = db.collection('users').where('count', '!=', '').where('nou', '==', false).get().then(snapshot => {
      const promises = [];
      snapshot.forEach(doc => {
        promises.push(doc.ref.update({ 'count': '' }));
      });
    });
    return Promise.all(promises)
}



export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
   const promise = clearCountField().then(resolve,reject); 
});

what do I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2025-01-30 22:27:09

循环中创建的承诺永远无法解决,因为promise.all()正在查询完成之前返回。这样修复它:

async function clearCountField() {
  const query = db.collection('users').where('count', '!=', '').where('nou', '==', false);
  return query.get().then(snapshot => {
    const promises = snapshot.docs.map(doc => doc.ref.update({ 'count': '' }));
    return Promise.all(promises)
  }
}

您的导出功能中的然后是多余的。只需从clearcountfield()返回承诺

export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
    return clearCountField();
});

The promises created in the forEach loop are never resolved because Promise.all() is being returned before the query completes. Fix it like this:

async function clearCountField() {
  const query = db.collection('users').where('count', '!=', '').where('nou', '==', false);
  return query.get().then(snapshot => {
    const promises = snapshot.docs.map(doc => doc.ref.update({ 'count': '' }));
    return Promise.all(promises)
  }
}

The then in your export function is superfluous. Just return the promise from clearCountField()

export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
    return clearCountField();
});
北城半夏 2025-01-30 22:27:09

更改以下内容:

export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
    const promise = clearCountField().then(resolve,reject); 
  }
);

对此:

export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
    return clearCountField().then(resolve,reject); 
  }
);

使一个问题消失了...但是在测试函数时,错误:ReferenceError:未定义的承诺仍然保留...

Changing the following:

export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
    const promise = clearCountField().then(resolve,reject); 
  }
);

To this:

export const ClearCountField24h = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/Bucharest') //  timezone 
  .onRun((context) => {
    return clearCountField().then(resolve,reject); 
  }
);

Made one issue go away... but when testing the function the Error: ReferenceError: promises is not defined still remains...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文