Firebase Node.js则不是函数

时间:2018-10-07 14:04:44

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

我正在调用一个云函数,并且收到此错误:不知道该怎么办?我尝试删除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);
    });
});

1 个答案:

答案 0 :(得分:2)

您缺少get()。所以:

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

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