从firebase和Unique键中检索数据

时间:2018-02-20 23:17:24

标签: javascript node.js firebase express

我会从firebase数据库中检索数据。这是我数据库的架构

datatable-18f93
  -L5pD-jh_Xpqq6bq2eDS
    text: "HELEO"
  -L5pDT-IpB4BsFMiABv_
    text: "HELEO"
  -L5pPwXnuTsSXN9pXpJ5
    text: "popopo"

我创建了获取数据的获取路径

router
  .route("/")
  .get(function (req, res, err) {
    console.log("test", req.body)

    // Get a database reference to our posts
    var db = admin.database();
    var ref = db.ref("/");

    // Attach an asynchronous callback to read the data at our posts reference
    ref.on("value", function (snapshot) {
      console.log(snapshot.val())
      res.send(snapshot.val());

    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });
  });

此路线还打印唯一键

{ '-L5pD-jh_Xpqq6bq2eDS': { text: 'HELEO' },
  '-L5pDT-IpB4BsFMiABv_': { text: 'HELEO' },
  '-L5pPwXnuTsSXN9pXpJ5': { text: 'popopo' } }

我想要的只是没有id的对象。我该怎么办? 这是一个例子

{ text: 'HELEO' },
{ text: 'HELEO' },
{ text: 'popopo' }

1 个答案:

答案 0 :(得分:2)

您需要遍历快照并将obj元素推送到新数组

ref.once("value", function (snapshot) {
    var list = [];
    snapshot.forEach(function(elem) {
        list.push(elem.val());
    });
    res.send(list);
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});