I am using a real-time database and have the following structure:
{
"users": {
"1234": {
"name": "Joe",
"externalId": "384738473847",
},
"5678": { ... },
"5555": { ... }
}
}
I now want to access a user via the externalId
and I am using the following to access this object:
const snapshot = await admin.database().ref(`/users/`).orderByChild('externalId').equalTo(`${someId}`).once('value')
This will return me the following:
{
"1234": {
“name”:Joe,
“externalId:”384738473847"
}
}
I would now would like to access the object (containing the name
and externalId
) without knowing the id (which is 1234).
I currently have this solution:
const rootValue = Object.keys(userSnapshot.val())[0]
const user = userSnapshot.val()[rootValue]
Which works, but I read that this is not the optimal way to do this. Is there a better way to access the object?