云功能->比较时间戳

时间:2019-02-26 12:44:38

标签: javascript firebase google-cloud-firestore firebase-admin

我正在尝试创建一个云函数,其中该函数删除“链接”集合中“ eindTijd”(结束时间)时间戳比现在更旧的每个项目。

该函数在每次数据库写入时执行,并且完全没有错误,但没有执行我打算做的事情。真让我发疯!

我怀疑错误在oldItemsQuery中,但是无法找出问题所在。任何帮助将不胜感激!

“ eindTijd”字段是使用Date函数生成的,在Firestore中被认为是有效的时间戳。

  'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);

exports.verwijderOudeNodes = functions.firestore
.document('/links/{pushId}')
.onWrite(() => {
  const now = Date.now();
  let oldItemsQuery = firestore.collection('links').where('eindTijd', '<=', now);
  return oldItemsQuery.get().then((snapshot) => {
    // create a map with all children that need to be removed
    let batch = firestore.batch();
    snapshot.forEach(child => {
      batch.delete(child.ref);
    });
    // execute all updates in one go and return the result to end the function
    return batch.commit();
  });
});

1 个答案:

答案 0 :(得分:1)

我不确定您通过now的方式是否正确。基于Firebase documentation on writing its various data types,我希望:

const now = admin.firestore.Timestamp.fromDate(new Date())
相关问题