我的数据是这样的:
"orders":
{
"-L6ue92e4DQ7Mm-veRxN" : {
"customer" : {
"address" : {
"country" : "",
"postCode" : "CB2 3AW",
"street" : "86 High Street"
},
"email" : "poo@gmail.com",
"name" : "Pooh"
},
"deliveryMethod" : "fastest",
"ingredients" : [ {
"Ingredient" : "cheese",
"Price" : 0.3,
"Quantity" : 1
}, {
"Ingredient" : "meat",
"Price" : 1,
"Quantity" : 2
} ],
"price" : 6.3
},
"-L6ueB632CCTKhGbbGLC" : {
"customer" : {
"address" : {
"country" : "",
"postCode" : "CB2 3AW",
"street" : "86 High Street"
},
"email" : "poo@gmail.com",
"name" : "Pooh"
},
"deliveryMethod" : "fastest",
"ingredients" : [ {
"Ingredient" : "relish",
"Price" : 0.15,
"Quantity" : 1
}, {
"Ingredient" : "bacon",
"Price" : 0.4,
"Quantity" : 1
}, {
"Ingredient" : "meat",
"Price" : 1,
"Quantity" : 2
} ],
"price" : 6.55
},
"-L6ueD9A2VuZUrM1hAFf" : {
"customer" : {
"address" : {
"country" : "",
"postCode" : "CB2 3AW",
"street" : "86 High Street"
},
"email" : "poo@gmail.com",
"name" : "Pooh"
},
"deliveryMethod" : "fastest",
"ingredients" : [ {
"Ingredient" : "meat",
"Price" : 1,
"Quantity" : 2
}, {
"Ingredient" : "salad",
"Price" : 0.2,
"Quantity" : 1
} ],
"price" : 6.2
}
}
我想选择电子邮件地址符合我的条件的订单,然后只返回配料节点。
我可以过滤电子邮件,然后像这样获取整个订单节点:
db.ref('orders')
.orderByChild("customer/email")
.equalTo(req.query.email)
.once('value')
并返回已过滤的订单
但equalTo返回一个Query。 ref是对该查询位置的引用。所以我期待能够做到这一点:
db.ref('orders')
.orderByChild("customer/email")
.equalTo(req.query.email)
.ref
.child('ingredients')
.once('value')
但返回null。由于我是新手(从SQL迁移),我不知道该怎么做。我可能会在.on的.then中做一些事情,但这似乎效率低下。我是否以错误的方式解决了这个问题?
另外,我是否必须执行orderByChild?我可以不在没有订购的情况下过滤电子邮件吗?
这是React SPA网络应用程序的全部内容。
答案 0 :(得分:2)
您需要根据查询的内容更改数据库的结构,这不会起作用:
db.ref('orders')
.orderByChild("customer/email")
.equalTo(req.query.email)
.ref
.child('ingredients')
.once('value')
你也只能使用一个查询(一个orderByChild()
),而过滤时你必须使用orderbychild。
你有这样的不同查询:
orderByChild("customer").equalTo(namehere);
orderByChild("time").startAt(5).endAt(5\uf8ff);
试试这个数据库:
customers
customerid
email:poo@gmail.com
name:Pooh
address
country:countryname
postCode"CB2 3AW
street:86 High Street
customerid2
/*some data here for other customer */
orders
orderid
customeremail: poo@gmail.com
deliveryMethod:fastest
Ingredient: cheese
Price: 0.3
Quantity : 1
orderid1
customeremail: poo@gmail.com
deliveryMethod:fastest
Ingredient : meat
Price: 1
Quantity: 2
现在你可以这样做:
firebase.database().ref().child("orders").orderByChild("customeremail").equalTo("poo@gmail.com").once('value').then(function(snapshot) {
snapshot.forEach(function(child) {
var ingredients=child.val().Ingredient;
)};
)};