To locate the drivers with the specified ID, you will need to execute a query:
firebase.database().ref('Driver')
.orderByChid('driverId')
.equalTo('1')
.once('value')
.then(function(snapshot) {
snapshot.forEach(function(driverSnapshot) {
console.log(driverSnapshot.val());
});
})
A query may return multiple child nodes, hence the use of snapshot.forEach
to handle potentially varied results.
If the ID is unique in your scenario, it is recommended to utilize that as the key in your Driver
node.
Driver
id1
location
0: 9.086333699999999
1: 7.459455999999999
In this setup, the retrieval becomes a straightforward lookup (instead of a query):
firebase.database().ref('Driver')
.child('id1')
.once('value')
.then(function(snapshot) {
console.log(snapshot.val());
})
As there can only exist one node corresponding to a specific key in this structure, the forEach
loop is no longer necessary.