In my current project, I am utilizing a real-time database with the following data structure:
{
"users": {
"1234": {
"name": "Joe",
"externalId": "384738473847",
},
"5678": { ... },
"5555": { ... }
}
}
My goal is to access a user based on their externalId
, and I have implemented the following logic for this purpose:
const snapshot = await admin.database().ref(`/users/`).orderByChild('externalId').equalTo(`${someId}`).once('value')
This code snippet returns the desired object containing user information as follows:
{
"1234": {
“name”: Joe,
“externalId:” 384738473847"
}
}
Now, I am seeking an alternative method to access the object (with the name
and externalId
) without prior knowledge of the id (1234).
Currently, I have a workaround in place:
const rootValue = Object.keys(userSnapshot.val())[0]
const user = userSnapshot.val()[rootValue]
Although this solution functions correctly, I came across information suggesting that there may be a more efficient way to achieve this. Are there any improved methods for accessing the object?