[英]how to get object of a nested child
[
{
"childKey": "o4oqRIVA3bdY33",
"childData": {
"serviceCity": "Santa Rosa",
"serviceDate": {
"2018-01-31": {
"clientId": "Y8valE4BUynL3y",
"Booked": false,
"shift": "Night"
},
"2018-02-02": {
"clientId": "Y8va12asdynL3y",
"Booked": false,
"shift": "Night"
}
},
"serviceProvince": "Laguna"
}
}
]
這就是我當前的響應。我有一個變量,用於保存由ajax傳遞的數據。 var startDate = '2018-01-31'
如果serviceDate
匹配/具有相同的startDate
我想做的就是得到此響應
router.get('/newWatch/:uuid',function(req, res){
var avail = database.ref('availableWatch');
var startDate = req.params.uuid;
avail.once('value', function(snapshot){
var promises = [];
snapshot.forEach(function(childSnapshot){
var childData = childSnapshot.val().serviceDate;
var Datestarts = childData[startDate];
console.log(Datestarts);
});
res.json(promises)
});
});
但問題是我可以獲取整個數據,但不能從serviceDate中獲取數據。 我不知道使用forEach是正確的事情..我試圖做的是獲取具有serviceDate of 2018-01-31..
的availableWatch serviceDate of 2018-01-31..
您錯過了這個: childSnapshot.childData.serviceDate
。
這應該為您工作:
snapshot.forEach(function(childSnapshot){
var childData = childSnapshot.childData.serviceDate;
var Datestarts = childData[startDate];
console.log(Datestarts);
});
據我從您的問題中了解到,僅當startDate匹配serviceDate中的日期之一時,才想執行AJAX。
您可以使用Object.keys()
從響應中獲取serviceDate的鍵,並相應地進行代碼處理。
var response=[
{
"childKey": "o4oqRIVA3bdY33",
"childData": {
"serviceCity": "Santa Rosa",
"serviceDate": {
"2018-01-31": {
"clientId": "Y8valE4BUynL3y",
"Booked": false,
"shift": "Night"
},
"2018-02-02": {
"clientId": "Y8va12asdynL3y",
"Booked": false,
"shift": "Night"
}
},
"serviceProvince": "Laguna"
}
}
]
然后
var serviceDateData = Object.keys(response [0] .childData.serviceDate);
serviceDateData
將包含["2018-01-31", "2018-02-02"]
現在,您可以使用邏輯來實現所需的功能。 希望對您有幫助
嘗試以下
router.get('/newWatch/:uuid',function(req, res){
var avail = database.ref('availableWatch');
var startDate = req.params.uuid;
avail.once('value', function(snapshot){
var promises = [];
snapshot.forEach(function(childSnapshot){
promises.push(childSnapshot.childData.serviceDate[startDate]);
});
res.json(promises)
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.