Firebase:如何使用ID从两个表中检索数据 [英] Firebase: How to retrieve data from two tables using id
问题描述
< img src =https://i.stack.imgur.com/gIjQF.pngalt =购物车模式>
用户购物车,我使用了以下内容:
var user_id =user1; //临时初始化
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不是函数
// var data = refMenu.child(item_id).exportval();
// exportval不是函数
// var data = $ firebase(refMenu.child(item_id))。$ asArray();
//给我一个对象数组,也就是属性 - OK!但是下一步做什么?
//console.log(\"DATA\",data);
console.log(Item,item_id ++ qty);
});
});
如何使用 item_id 来检索
这是从多个表中进行数据检索的正确方法吗?
更新:
使用 on()函数,我设法得到项目属性。
refMenu.child (value,function(snapshot))
console.log(Price,snapshot.val()。price);
});
但是有没有更好的执行相同。
是否有更好的方法从服务器端检索项目的特定属性。
像在SQL中一样
$ p $ SELECT menu name,price,... from menu;
注意: .on 'event',callback)
方法会在每次事件触发时调用你的回调函数。如果你需要从一个引用中检索数据,你应该使用: .once('event',callback)$ c $注意2:snapshot.val()会给你一个JSON对象,你可以指定给变量
p>我会这样做:
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 )
});
});
});
希望有帮助;)
I come from an SQL background and recently started using Firebase for building an ionic shopping cart. This is the database schema:
To retrieve a user's cart, i used the following
var user_id="user1"; // Temporary initialised
var refCart = new Firebase("https://testing.firebaseio.com/cart");
var cart=$firebase(fireBaseData.refCart().child(user_id)).$asArray();
This gives the result:
item1 1
item2 2
item3 5
So tried using 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);
});
});
How can i use item_id to retrieve item details.
Is it the correct way of doing data retrieval from multiple tables?
Update:
Using on() function , i managed to get the item attributes.
refMenu.child(item_id).on("value", function(snapshot) {
console.log("Price",snapshot.val().price);
});
But is there any better implementation for the same.
Is there any better ways to retrieve (from the server side) specific attributes for the item. Like in SQL
SELECT name,price, ... from menu;
NOTE: .on('event', callback)
method will call your callback every time the event is fired.
If you need to retrieve data from a reference once, you should use: .once('event', callback)
NOTE2: snapshot.val() will give you a JSON object that you can assign to a variable
I would do it this way:
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)
});
});
});
Hope it helps ;)
这篇关于Firebase:如何使用ID从两个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!