【问题标题】:Firestore increment is undefined?Firestore 增量未定义?
【发布时间】: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


【解决方案1】:

老实说不知道为什么这不起作用,但我让它与这段代码一起工作

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp();

exports.waitReportCreatedJS = functions.firestore
    .document('/contests/{contestId}/{tickets}/{ticketId}') 
    .onWrite((snapshot, context) => { 

    admin.firestore().doc('contests/' + context.params.contestId)
        .update({ points: admin.firestore.FieldValue.increment(1) })
        .then(() => console.log('this will succeed'))
        .catch(() => 'obligatory catch');
});

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 2021-09-25
    • 2014-12-26
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2019-09-20
    相关资源
    最近更新 更多