function isDisplayNameTaken(name) {
firebase
.database()
.ref()
.child("users")
.orderByChild("username")
.equalTo(name)
.once("value")
.then((snapshot) => {
return snapshot.exists();
});
}
我一直在尝试在注册用户之前检查数据库中是否已经存在用户名,但是返回不起作用,函数(快照)中的任何内容也不会更改其他全局变量来解决该问题。
if(isDisplayNameTaken($userDisplayNameField.val())) {
numberOfChecksPassed += 1
}
这是无法通过的条件。我已经进行了研究,并在最近5个小时内一直试图解决问题。提前致谢
首先,您不能像在简单if
语句中那样使用promise结果。您应该执行以下操作:
isDisplayNameTaken($userdisplayNameField.val()).then(taken => {
console.log('Is it taken?', taken);
});
在您的简单示例中,if
该if
语句将promise对象作为其条件而不是已解析的值进行检查。
为此,isDisplayNameTaken
需要返回一个承诺(不是)。
function isDisplayNameTaken(name) {
return firebase // Notice the return here.
.database()
.ref()
.child("users")
.orderByChild("username")
.equalTo(name)
.once("value")
.then((snapshot) => {
return snapshot.exists(); // This return will resolve the promise but you are not returning the resolved promise out of the function.
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句