[英]Why the object returned from a fetch function is a function outside it?
I don't get why data
type is an object inside my function but outside is a function... Any ideas why ?我不明白为什么
data
类型是我的函数内部的对象,但外部是一个函数......任何想法为什么? 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();
Read the documentation: https://firebase.google.com/docs/reference/node/firebase.database.Reference#on阅读文档: https : //firebase.google.com/docs/reference/node/firebase.database.Reference#on
The .on method will return anything, not your DataSnapshot. .on 方法将返回任何内容,而不是您的 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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.