Firebase函数onWrite未被调用 [英] Firebase function onWrite not being called

查看:58
本文介绍了Firebase函数onWrite未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firebase Functions实现触发器,该触发器复制数据库中的某些数据.我想在votes/user/vote观看所有添加的内容,结构为:

I'm trying to implement a trigger using Firebase Functions, that duplicates some data in the database. I want to watch all additions at votes/user/vote, the structure is:

我尝试的代码是:

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;
    });

但是我什至无法触发它.任何线索我的函数有什么问题吗?我敢打赌,这与参考资料有关,但我无法弄清楚到底是什么.

But I can't even get it triggered.. Any clues what's wrong with my function? I bet is related with the reference, but I can't figure out what exactly.

推荐答案

现在,该功能是通过写入

Right now, the function is triggered by a write to

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

这将由类似这样的触发:

which would be triggered by something like this:

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

但是您并没有在寻找voteId的孩子,而是在寻找{userId}的任何孩子,例如-33,因此您需要使用通配符:

But you're not looking for the child voteId, you're looking for any child of {userId}, such as -33, so you need to use a wildcard:

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

这篇关于Firebase函数onWrite未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆