我将从 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);
});
});
这条路线还打印唯一的 key
{ '-L5pD-jh_Xpqq6bq2eDS': { text: 'HELEO' },
'-L5pDT-IpB4BsFMiABv_': { text: 'HELEO' },
'-L5pPwXnuTsSXN9pXpJ5': { text: 'popopo' } }
我想要的只是没有id的对象。我怎样才能得到那个? 这是一个例子
{ text: 'HELEO' },
{ text: 'HELEO' },
{ text: 'popopo' }
最佳答案
您需要循环遍历快照并将 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);
});
关于javascript - 从 firebase 和唯一键检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48895769/