【问题标题】:Get placeholder values on firebase onWrite database function在firebase onWrite数据库函数上获取占位符值
【发布时间】:2017-10-11 05:06:04
【问题描述】:

我想实现一个计数器函数来增加一个帖子的点赞数,这里是代码(取自 firebase 计数器函数示例)我已经修改了一点,我如何获得指定的占位符值数据库参考(cid,coid)?

exports.countCommentChange = functions.database.ref('/clipComments/{cid}/{coid}').onWrite(event => {

    const db = admin.database();
    const clipRef = db.ref(`/clips/${cid}`); // <- how do I get CID?

    const countRef = clipRef.child('comments');

    return countRef.transaction(current => {
        if (event.data.exists() && !event.data.previous.exists()) {
            return (current || 0) + 1;
        }
        else if (!event.data.exists() && event.data.previous.exists()) {
            return (current || 0) - 1;
        }
    }).then(() => {
        console.log('Counter updated.');
    });
});

我的数据库结构如下:

clips: {
    clipId: {
        name: "my awesome clip",
        likes: 0,
        comments: 0
    }
},
clipComments: {
    clipId:
    {
        commentTimestamp: {
           comment: "awesome video!"
        }
    }
}

【问题讨论】:

    标签: node.js firebase firebase-realtime-database


    【解决方案1】:

    如果您从onWrite 侦听器中console.log 您的event,您将能够在仪表板控制台中看到存储在此对象中的全部数据。

    您应该注意到开头有一个名为params 的对象。该对象将所有占位符变量存储在导出的函数中。

    在您的情况下,您应该能够使用event.params.cid 访问您的占位符。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 2019-08-10
      • 1970-01-01
      • 2020-08-24
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      相关资源
      最近更新 更多