我有此代码,我只需要返回一个uid,以便以后可以使用Id进行Firestore操作
$(document).ready(function () {
var uid;
auth.onAuthStateChanged(function (user) {
if (user != null) {
uid = user.uid;
console.log(typeof uid)//returns a string ;
}
});
console.log(uid);
function fetch() {
var docRef = db.collection("Users").doc(uid);
docRef.get().then(function (doc) {
console.log(doc.data);
}).catch(function (error) {
console.log("Error getting document:", error);
});
}
});
我如何存储uid以便以后检索它以进行数据库操作。当前,运行该check函数将返回uid未定义的错误。如果我尝试登录uid控制台也一样。我猜该函数在侦听器解析之前运行,但是我将如何解决此问题。由于用户之间的互联网连接速度不同,设置计时器也无济于事
(添加代码)
check1();
check3();
//fetch();
function check1() {
if (typeof uid === 'undefined') {
console.log("not ready");
return false
}
}
function check3() {
if (check1 == false) {
check1();
} else {
console.log("ready"); //here this logs ready
console.log(typeof uid); //then here it logs "undefined" still
fetch(); //so this function call brings the error
}
}
function fetch() {
var docRef = db.collection("Users").doc(uid);
docRef.get().then(function (doc) {
console.log(doc.data();
}).catch(function (error) {
console.log("Error getting document:", error);
});
}
check3如果uid未定义,为什么函数准备好记录日志。我还需要隐藏特定的链接,直到uid具有值为止。我该如何改善上述代码而无法隐藏链接
繁华开满天机
相关分类