【问题标题】:Firebase Node.js then is not a functionFirebase Node.js 不是一个函数
【发布时间】:2019-03-12 08:32:48
【问题描述】:

我有一个正在调用的云函数并收到此错误:不确定该怎么做?我尝试删除 return 语句并返回 Primise.all() 并返回 batch.commit(),但都失败了我还必须将 eventsRef 设为 var 而不是 const,不知道为什么。

错误:

TypeError: eventsRef.where(...).then is not a function
    at exports.deleteOldEventLocations.functions.https.onRequest (/user_code/index.js:235:6)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /var/tmp/worker/worker.js:689:7
    at /var/tmp/worker/worker.js:673:9
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

功能:

exports.deleteOldEventLocations = functions.https.onRequest((req, res) => {
  const db = admin.firestore();
  var eventsRef = db.collection('events');
  const currentDate = new Date();
  const endDate = new Date(currentDate.getTime + 2 * 60 * 60 * 1000);
  const batch = db.batch();
  const fieldValue = db.FieldValue;

  var query = eventsRef.where('startDate', '<', endDate)
    .then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
            let eventRef = eventsRef.doc(doc.id);
            batch.update(eventRef, { g: fieldValue.delete() });
            batch.update(eventRef, { l: fieldValue.delete() });
        });
        batch.commit();
        return;
    })
    .catch(err => {
        console.log('Error getting documents', err);
    });
});

【问题讨论】:

    标签: node.js firebase promise google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您缺少get()。所以:

    var query = eventsRef.where('startDate', '<', endDate)
      .get()
      .then(snapshot => {
        snapshot.forEach(doc => {
          ...
    

    另请参阅getting multiple documents from a collection 上的文档。

    【讨论】:

    • 谢谢!有没有办法做我正在做的比较日期,因为我的日期存储为firebase上的时间戳,或者我需要将它们存储为秒而不是比较,因为我现在收到上面那个查询的错误跨度>
    • @Michael 看看stackoverflow.com/questions/52221830/…,它可能会有所帮助。
    猜你喜欢
    • 2016-09-10
    • 2017-10-25
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多