简体   繁体   English

在其他 function 中调用异步 function

[英]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.

相关问题 通过 PUT API 调用异步调用 function - Call async function via PUT API call 如何在Typescript中调用这种类型的async function - How to call this type of async function in Typescript 异步 function 调用不会对父调用 function 产生任何结果 - Async function call not yielding any results to parent calling function 如何在异步谷歌云 function 中调用第二个 function - How to call a second function within async google cloud function 看似同步执行的异步 function 调用 (C#) - Async function call seemingly executing synchronously (C#) 如何在运行另一个之前等待异步 function - How to wait for a async function before run the other one 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function 在 Unity 中,如何调用 firebase 异步 function 并将值返回给调用 function? - In Unity how do you call a firebase async function and return the value to a calling function? 异步 function 本机反应 - Async function react native useEffect() function 内部的 Firestore 异步调用导致在路由周围导航时出错 - Firestore async call inside useEffect() function causes error when navigating around routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM