我不明白为什么data
类型是我的函数内部的对象,但外面是一个函数…知道为什么吗?谢谢。
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();
阅读文档:https://firebase.google.com/docs/reference/node/firebase.database.Reference#on
on方法将返回任何东西,而不是你的DataSnapshot。更好的方法是使用回调。下面是一个例子: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
});