Firebase:如何使用 id 从两个表中检索数据
Firebase: How to retrieve data from two tables using id
我有 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 检索 项目详细信息。
从多个表中检索数据的正确方法吗?
更新:
使用 on() 函数,我设法获取了项目属性。
refMenu.child(item_id).on("value", function(snapshot) {
console.log("Price",snapshot.val().price);
});
但是有没有更好的实现呢。
是否有更好的方法来检索(从服务器端)项目的特定属性。
喜欢 SQL
SELECT name,price, ... from menu;
注意:.on('event', callback)
方法将在每次触发事件时调用您的回调。
如果您需要一次从引用中检索数据,您应该使用:.once('event', callback)
注意 2:snapshot.val() 将为您提供一个 JSON 对象,您可以将其分配给变量
我会这样做:
refCart.child(user_id).on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var item_id = childSnapshot.name();
var qty = childSnapshot.val();
refMenu.child(item_id).once("value", function(snapshot) {
var item = snapshot.val()
console.log(item.name +' '+ item.price)
});
});
});
希望对您有所帮助 ;)
我有 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 检索 项目详细信息。
从多个表中检索数据的正确方法吗?
更新:
使用 on() 函数,我设法获取了项目属性。
refMenu.child(item_id).on("value", function(snapshot) {
console.log("Price",snapshot.val().price);
});
但是有没有更好的实现呢。
是否有更好的方法来检索(从服务器端)项目的特定属性。 喜欢 SQL
SELECT name,price, ... from menu;
注意:.on('event', callback)
方法将在每次触发事件时调用您的回调。
如果您需要一次从引用中检索数据,您应该使用:.once('event', callback)
注意 2:snapshot.val() 将为您提供一个 JSON 对象,您可以将其分配给变量
我会这样做:
refCart.child(user_id).on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var item_id = childSnapshot.name();
var qty = childSnapshot.val();
refMenu.child(item_id).once("value", function(snapshot) {
var item = snapshot.val()
console.log(item.name +' '+ item.price)
});
});
});
希望对您有所帮助 ;)