【发布时间】:2020-06-27 20:52:17
【问题描述】:
我有以下触发器,很难对父字段进行增量。 有什么建议吗?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const fieldValue = admin.firestore.FieldValue;
admin.initializeApp();
exports.onTicketCreate = functions.firestore
.document('/contests/{contestId}/{tickets}/{ticketId}')
.onWrite((snapshot, context) => {
functions.firestore.document('contests/' + context.params.contestId).update({
points: fieldValue.increment(1)
});
});
错误
TypeError: Cannot read property 'increment' of undefined
at exports.onTicketCreate.functions.firestore.document.onWrite (/workspace/lib/index.js:27:48)
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:132:23)
at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28)
at process._tickCallback (internal/process/next_tick.js:68:7)
【问题讨论】:
-
未定义的不是
increment,而是FieldValue。我不立即明白为什么会这样。作为一个测试,你可以试试points: admin.firestore.FieldValue.increment(1)吗? -
我还要指出,您不能使用
functions常量查询 Firestore。那只是为了构建功能。您将需要使用admin.firestore()来获取对可以执行查询的 Firestore 实例的引用。
标签: javascript node.js firebase google-cloud-firestore firebase-admin