简体   繁体   English

为什么 async await 在这个 function 中不起作用?

[英]Why does async await not work in this function?

This might be a bit of a beginner question, but I can't figure this out:这可能是一个初学者问题,但我无法弄清楚:

I'm doing an asyncronous database call in which I want to get an object from the database and asign this object to the window object (window.naamID).我正在执行异步数据库调用,其中我想从数据库中获取 object 并将此 object 分配给 window object (window.naamID)。

Next I want to run a function where I use the object assinged to the window object (making it global) and use it as a database query (.where("Vraagsteller", "==", naamID)).接下来我想运行一个 function,其中我使用分配给 window object(使其成为全局)的 object 并将其用作数据库查询(.where(“Vraagsteller”,“==”,naamID))。

The problem, of course, is that the second function starts running before the first function has fnished (because of it's asyncronous nature).当然,问题是第二个 function 在第一个 function 完成之前开始运行(因为它是异步的)。

I wanted to overcome this problem by wrapping the second function in a syncronous function and await the first function.我想通过将第二个 function 包装在同步 function 中并等待第一个 function 来克服这个问题。

This doesn't seem the work.这似乎不是工作。 The console says the object asigned to the window object is undefined.控制台显示分配给 window object 的 object 未定义。

控制台日志的快照

This is the code:这是代码:

 function constructAuthMenu(){
auth.onAuthStateChanged(User =>{

    const userRef = db.collection("Vitaminders")
    .doc(User.uid);
    userRef.get()
    .then(function(doc) {

        const naamID = doc.data().Gebruikersnaam;
        const ID = doc.data().ID
        const naam = naamID.replace(ID, "")
        const profilePic = doc.data().Profielfoto

        window.naamID = naamID
     });
  });
}; 

constructAuthMenu()

 async function getNewReactions(){

        await constructAuthMenu()

      db.collectionGroup("Reactions")
      .where("Vraagsteller", "==", naamID)
      .where("New", "==", 
       "Yes").get().then(querySnapshot => {
        querySnapshot.forEach(doc => {

       *Do stuff*

   });
});

What am I doing wrong?我究竟做错了什么? Or doesn't async await work like this?或者异步等待不是这样工作的吗?

If constructMenuFunction actually exists elsewhere in your code, and returns a Promise, then your problem is that you're missing parentheses on your function call: await constructMenuFunction();如果constructMenuFunction实际上存在于您的代码中的其他地方,并返回 Promise,那么您的问题是您在 function 调用中缺少括号: await constructMenuFunction();

If constructMenuFunction does not exist elsewhere in your code, and the first function preceding the async function is actually what you wish to invoke, your problem is that the first function does not return a Promise and your async function invokes the wrong method and without parentheses.如果代码中的其他地方不存在constructMenuFunction ,并且异步 function 之前的第一个 function 实际上是您想要调用的,那么您的问题是第一个 function 不返回 Promise 并且您的异步 function 调用错误的方法并且没有父母。

async function constructAuthMenu() {
  return await auth.onAuthStateChanged(async User => {
    try {
      const userRef = db.collection("Vitaminders").doc(User.uid);
      const doc = await userRef.get();
      const naamID = doc.data().Gebruikersnaam;
      const ID = doc.data().ID;
      const naam = naamID.replace(ID, "");
      const profilePic = doc.data().Profielfoto;
      return naamID;
    } catch(err) {
      console.error(err);
    }
 });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM