如何在一个传递函数中使用 await

huangapple go评论61阅读模式
英文:

How to use await in a passthrough function

问题

我试图在一个函数内使用await,我认为它是一个透传函数。

我使用createUserWithEmailAndPassword创建一个用户,然后将cred对象传递到成功创建用户后要执行的操作。如果用户成功创建,然后代码会写入用户信息并在数据库中设置进一步注册信息的占位符,以便用户完成注册。

问题是,页面在数据库写入完成之前就刷新到下一个注册阶段(不同的页面)。

我不知道如何在.then()部分内使用await,如果数据库写入不在那部分,cred对象传入的cred.user.uid将无法工作,这是至关重要的。

以下是我的代码:

await createUserWithEmailAndPassword(auth, email, password)
  .then((cred) => {
    console.log('User Created:', cred.user);

    // 创建替代用户文档
    setDoc(doc(db, 'users', cred.user.uid, 'userInfo', 'signupInfo'), {
      dob: dob,
      signupAge: age,
      tosAccepted: true,
    });

    setDoc(doc(db, 'users', cred.user.uid, 'technical', 'signup'), {
      passedGo: false,
      userDetailsComplete: false,
      phoneNoComplete: false,
      initialized: false
    });

    signupForm.reset();
    document.querySelector('#accCreated').removeAttribute('style', 'display: none;');
  })

我尝试将数据库更新放在.then()部分之外,但我需要cred数据才能工作。

我还尝试在.then()部分内创建一个异步函数,但这也会将函数的内容与cred对象分开。

我还尝试将数据库更新放在onAuthStateChanged函数内,希望它会提供cred信息/uid等。但那没有起作用。我认为用户在注册时还没有登录。

英文:

I'm trying to use await inside a function I think it's a passthrough function.

I'm creating a user with 'createUserWithEmailAndPassword' then passing the 'cred' object into what to do if the user is created successfully. If the user is created succesfully the code then writes the user info and sets place holders for further signup info in the database ready for the user to comeplete sign up.

The problem is, the page is refreshing to the next phase of sign up (a different page) before the database writes are being completed.

I have no idea how to get await to work inside the '.then()' section and if the database writes arent in that part where the 'cred' object is being fed the 'cred.user.uid' won't work which is integral

Here is my code:

        await createUserWithEmailAndPassword(auth, email, password)
          .then((cred) => {
            console.log('User Created:', cred.user);

            //Create Stand-In User Doc
            setDoc(doc(db, 'users', cred.user.uid, 'userInfo', 'signupInfo'), {
              dob: dob,
              signupAge: age,
              tosAccepted: true,
            });

            setDoc(doc(db, 'users', cred.user.uid, 'technical', 'signup'), {
              passedGo: false,
              userDetailsComplete: false,
              phoneNoComplete: false,
              initialized: false
            });

            signupForm.reset();
            document.querySelector('#accCreated').removeAttribute('style', 'display: none;');
          })

I have tried putting the database updates outside of the .then() section but I need the 'cred' data for it to work.

I have also tried creating a async function iniside the .then() section but that also cuts the contents of the function off from the 'cred' object

I have also tried putting the database updates inside the 'onAuthStateChanged' function hoping that would provide the 'cred' info / uid etc. But that didnt work. I don't think the user is logged in at sign up

答案1

得分: 2

Combining await with then is not the way to go. await lets you specifically skip the then part of the asynchronous function and store the return of said function into a variable.

const cred = await createUserWithEmailAndPassword(auth, email, password);
console.log('User Created:', cred.user);

Now, assuming that setDoc is also async, you'd need to await those, too, so the reset doesn't get triggered before it's done.

await setDoc(...);
await setDoc(...);
signupForm.reset();

But to answer your other question: You can make a then function async like this:

.then(async (cred) => { ... })
英文:

Combining await with then is not the way to go. await lets you specifically skip the then part of the asynchronous function and store the return of said function into a variable.

const cred = await createUserWithEmailAndPassword(auth, email, password);
console.log('User Created:', cred.user);

Now, assuming that setDoc is also async, you'd need to await those, too, so the reset doesn't get triggerd before it's done.

await setDoc(...);
await setDoc(...);
signupForm.reset();

But to answer your other question: You can make a then function async like this:

.then(async (cred) => { ... })

huangapple
  • 本文由 发表于 2023年2月6日 16:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定