I don't get why data
type is an object inside my function but outside is a function... Any ideas why ? Thanks.
const fetchAllUsersData = async () => {
setLoading(true);
const userFromFirebase = await firebase.database().ref("users").orderByChild("nickname").equalTo(nickname).on("child_added", ((snapshot) => {
const data = snapshot.val();
console.log("data", typeof data); // show object
return data;
}));
console.log("userFromFirebase",typeof userFromFirebase); // show function
}
fetchAllUsersData();
CodePudding user response:
Read the documentation: https://firebase.google.com/docs/reference/node/firebase.database.Reference#on
The .on method will return anything, not your DataSnapshot. A better approach to this would be to use callbacks instead. Here is an example:
const fetchAllUsersData = async (callback) => {
setLoading(true);
const userFromFirebase = await firebase.database().ref("users").orderByChild("nickname").equalTo(nickname).on("child_added", ((snapshot) => {
const data = snapshot.val();
console.log("data", typeof data); // show object
callback(data);
}));
}
fetchAllUsersData((data) => {
console.log("data", typeof data); // still shows object
});