Reputation: 671
When I retrieve the Firebase real-time database snapshot, its value (snapshot.val())
comes with a key, cant access data in the document. This is the code.
const recordExist = await admin.database().ref("order").child("live_orders").orderByChild('driver_id').equalTo(driverId)
.limitToLast(1).once('value', function (snapshot) {
console.log('Live order exisitng : ' + JSON.stringify(snapshot.val()));
return snapshot.val();
});
and this the console log
Live order exisitng : {
"-M4E52NNntPP5BSHp0b-":{
"customerName":"Ruwan Perera",
"customerNo":"0775886998",
"deliveryAddress":"Galle Rd, Moratuwa 10400",
"deliveryLocation":{
"latitude":6.7798672,
"longitude":79.8831725
},
"distance_to_deliver":14791,
"distance_to_pickup":788,
"driver_id":"K9HA9HfLMEZC4qR1IDbjGbSscx13",
"duration_to_deliver":2228,
"duration_to_pickup":171,
"orderNo":"order-0001",
"order_id":"-M4E52AHF4nMwis0b6Wa",
"paymentMethod":"Cash On Delivery",
"pickupAddress":"603 Kotte Rd, Sri Jayawardenepura Kotte 10100",
"pickupLocation":{
"latitude":6.8903602,
"longitude":79.9032734
},
"status":"Rejected",
"totalPayment":"1000",
"totalQuantity":"5",
"total_distance":15579,
"total_duration":342
}
}
and I want to access "status" in it. and when I try to access the status it says undefined.
Upvotes: 0
Views: 153
Reputation: 671
I found a solution to my matter.
Attaching a value observer to a list of data will return the entire list of data as a single snapshot which you can then loop over to access individual children.
Even when there is only a single match for the query, the snapshot is still a list; it just contains a single item. To access the item, you need to loop over the result:
so my solution is
const existingRecords: any[] = [];
const recordExist = await admin.database().ref("order").child("live_orders").orderByChild('driver_id').equalTo(driverId).limitToLast(1).once('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var childData = childSnapshot.val();
console.log('child snapshot : ' + JSON.stringify(childData));
existingRecords.push(childData);
});
});
if (recordExist !== null) {
const status = existingRecords[0].status;
if (status !== 'Rejected') {
if (status !== 'Completed') {
console.log('driver ' + driverLive.firstName + ' ' + driverLive.lastName + ' is busy right now and status is ' + status);
continue;
}
}
}
and it worked for me.
Upvotes: 0
Reputation: 83058
You are mixing up async/await and callback approaches, which is not recommended.
So, with async/await, you should do as follows:
const dataSnapshot = await admin.database().ref("order").child("live_orders").orderByChild("driver_id").equalTo(driverId).limitToLast(1).once('value');
const status = dataSnapshot.val().status;
We simply call the val()
method of the DataSnapshot
, which extracts a JavaScript value and get the status property of this JavaScript Object.
If you want to transform it in an async function, you could do as follows (to be fined tune according to your exact needs):
async function isRecordRejected(driverId) {
const dataSnapshot = await admin.database().ref("order").child("live_orders").orderByChild("driver_id").equalTo(driverId).limitToLast(1).once('value');
const status = dataSnapshot.val().status;
return (status === "Rejected" ? true : false);
}
Finally, note that if you want to check the existence of a DataSnapshot
, you should use the exists()
method.
Upvotes: 1