【问题标题】:Firebase function onWrite not being called未调用 Firebase 函数 onWrite
【发布时间】:2018-03-28 04:19:17
【问题描述】:

我正在尝试使用 Firebase 函数实现触发器,该触发器会复制数据库中的一些数据。我想在votes/user/vote观看所有添加,结构是:

我试过的代码是:

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

admin.initializeApp(functions.config().firebase);

exports.duplicateFeedback = functions.database.ref('/votes/{userId}/voteId')
    .onWrite(event => {
        const userId = event.params.userId;
        const voteId = event.data.key();
        const vote = event.data.val();
        console.log(`New feedback from user ${userId}: { description: "${vote.feedback}", score: ${vote.rating}}`);

        return;
    });

但我什至无法触发它。任何线索我的功能有什么问题?我打赌与参考有关,但我无法弄清楚到底是什么。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    现在,该函数由写入触发

    exports.duplicateFeedback = functions.database.ref('/votes/{userId}/voteId')
    

    这会被这样的事情触发:

    votes: {
        00000_josemi_foo: {
            voteId: {
                // data
            }
        }
    }
    

    但是你不是在寻找voteId的孩子,你在寻找{userId}的任何孩子,比如-33,所以你需要使用通配符:

    exports.duplicateFeedback = functions.database.ref('/votes/{userId}/{voteId}')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 2018-05-04
      • 1970-01-01
      • 2019-01-31
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      相关资源
      最近更新 更多