[英]Call async function in other function
I am having a problem with asynchronous functions.我在使用异步函数时遇到问题。 I have the following function which works fine and basically searches into the firebase realtime database for a matching username:
我有以下 function 它工作正常并且基本上搜索到 firebase 实时数据库以查找匹配的用户名:
static async getSnapshot(fc: FormControl){
let isPresent:boolean = false;
await firebase.database().ref().child("users").orderByChild("username")
.equalTo(fc.value)
.once("value", snapshot => {
}).then((data)=> {
if(data.exists())
isPresent = true;
else
isPresent = false;
});
console.log(isPresent);
return isPresent;
}
The problem is when I call this function in another where I want to do other operations based on the result:问题是当我在另一个我想根据结果进行其他操作的地方调用这个 function 时:
static async validUsername(fc: FormControl){
try{
let bool:boolean =await this.getSnapshot(fc.value)
if(bool===true)
return ({validUsername: true});
else{
return (null);
}
}catch(e){
console.log(e)
}
}
The line:该行:
let bool:boolean =await this.getSnapshot(fc.value)
returns the following error:返回以下错误:
TypeError: Cannot read property 'getSnapshot' of undefined
How can I modify my function?如何修改我的function? Thanks in advance for response
预先感谢您的回复
this
refers to an instance usually. this
通常指的是一个实例。 Static methods don't belong to any instance so this
doesn't make sense in them. Static 方法不属于任何实例,因此
this
在它们中没有意义。
To fix your case, just use your class name instead of this
.要解决您的问题,只需使用您的 class 名称而不是
this
名称。 Eg例如
class APIHandlers {
static async getSnapshot {...}
static async validUsername(fc: FormControl){
try{
let bool:boolean = await APIHandlers.getSnapshot(fc.value);
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.