【问题标题】:Firebase Saving Object as Key to DatabaseFirebase 将对象保存为数据库的键
【发布时间】:2016-09-20 12:07:59
【问题描述】:
router.post('/postChef', function(req, res, next) {
    var userid = req.body.userID;
    var mealid = req.body.mealID;
    db.ref("chefs/" + userid).child("Meals").set({
        mealid : true
    });
    res.send('respond with a resource');
});

在上面的代码中,我不确定为什么 mealid 在 firebase 数据库中变成了“mealid”。我想要的是函数给出的mealid的字符串值。我怎样才能做到这一点?在定义的路径中,Meals 是一个子项,其中包含设置为 true 或 false 的所有 mealID 的 JSON 列表。

【问题讨论】:

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


    【解决方案1】:

    由于您在分配的属性的左侧使用术语“mealid”,因此它被解释为字符串而不是变量。

    要使用的值 mealid 作为属性的名称,请切换到“方括号”表示法:

    router.post('/postChef', function(req, res, next) {
        var userid = req.body.userID;
        var mealid = req.body.mealID;
        var obj = {};
        obj[mealid] = true;
        db.ref("chefs/" + userid).child("Meals").set(obj);
        res.send('respond with a resource');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多