【发布时间】:2016-10-20 19:10:48
【问题描述】:
我来自 SQL 背景,最近开始使用 Firebase 构建离子购物车。这是数据库架构:
为了检索用户的购物车,我使用了以下
var user_id="user1"; // Temporary initialised
var refCart = new Firebase("https://testing.firebaseio.com/cart");
var cart=$firebase(fireBaseData.refCart().child(user_id)).$asArray();
这给出了结果:
item1 1
item2 2
item3 5
所以尝试使用foreach()
var refMenu = new Firebase("https://testing.firebaseio.com/menu");
refCart.child(user_id).on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var item_id = childSnapshot.name();
var qty = childSnapshot.val();
//var data= refMenu.child(item_id).val();
// val is not a function
//var data= refMenu.child(item_id).exportval();
// exportval is not a function
//var data = $firebase (refMenu.child(item_id)). $asArray();
// Give me an array of objects , ie attributes - OK! But what to do next ?
//console.log("DATA",data );
console.log("Item",item_id+" "+qty);
});
});
如何使用 item_id 来检索 item details。
从多个表中检索数据是否正确?
更新:
使用on() 函数,我设法获取了项目属性。
refMenu.child(item_id).on("value", function(snapshot) {
console.log("Price",snapshot.val().price);
});
但是有没有更好的实现方式。
有没有更好的方法来检索(从服务器端)项目的特定属性。 就像在 SQL 中一样
SELECT name,price, ... from menu;
【问题讨论】:
标签: ionic-framework firebase firebase-realtime-database