I am fairly new to typescript and I have managed so far with it but I found a weird error/bug that I cannot figure out with async/await.
I have the following code snippet and my problem is that the 'await' clause doesn't work with the .once() function.
async function testFunction(deviceID:string){
const returnObject:{name:string, description:string, tokens:string[]}[] = [];
const deviceRef = admin.database().ref('deviceInfo/').orderByChild('id').equalTo(deviceID);
await deviceRef.once('value', async (payload) => {
const pushObject:{name:string, description:string, tokens:string[]} = {name:"", description:"", tokens:[]};
if(payload.exists()){
const devices = payload.val();
const keys = Object.keys(devices);
for(const key of keys){
pushObject.name = devices[key].name;
pushObject.description = devices[key].description;
pushObject.tokens = await getTokens(key);
returnObject.push(pushObject);
}
console.log("Return Object 1:", returnObject);
return returnObject;
}
else{
return null;
}
})
console.log("Return Object 2:", returnObject);
return returnObject;
}
This function always returns {name:" ", description: " ", tokens:[]} even though I have 'await' at the start of my deviceRef.once() function.
Also both console.logs print, "Return Object 1" prints the correct object with all the data retrieved from the DB but "Return Object 2" prints an empty array. So it is clear that the await is not waiting for the once() function to complete, I'm just not sure why this is?
Any help or advice would be appreciated