我正在调用一个云函数,并且收到此错误:不知道该怎么办?我尝试删除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);
});
});
答案 0 :(得分:2)
您缺少get()
。所以:
var query = eventsRef.where('startDate', '<', endDate)
.get()
.then(snapshot => {
snapshot.forEach(doc => {
...