简体   繁体   English

将Firebase方法转换为异步等待

[英]Convert A Firebase Method To Async Await

I have this firebase method that creates a firebase user using a email and password 我有这个使用电子邮件和密码创建Firebase用户的firebase方法

async register(name, email, password,type) {
    let id;
    const createUser = this.functions.httpsCallable('createUser');

    return await this.auth.createUserWithEmailAndPassword({email,password })
      .then((newUser)=>{
        id = newUser.user.uid;
        newUser.user.updateProfile({
          displayName: name
        })
      })
      .then(()=>{
        createUser({
          id:id,
          name:name,
          email:email,
          type:type
        })
      })
  }

It also adds the user to a firestore collection using a cloud function that takes the user details and user type. 它还使用使用用户详细信息和用户类型的云功能将用户添加到Firestore集合中。

I have 3 Promises ( 我有3个承诺(

  1. createUserWithEmail...()
  2. updateUserProfile()1
  3. createUser()

) which Are dependent on each other.. How do I use them in one function? )彼此依赖。.如何在一个功能中使用它们?

NB : Couldn't use the functions.auth.user().onCreate() method because of the user type field 注意 :由于用户类型字段,无法使用functions.auth.user().onCreate()方法

How do I write this method without using the .then() ? 如何不使用.then()编写此方法? Some users aren't appearing in the database 某些用户未出现在数据库中

to remove .then simply use await "better" 删除.then简单地用await “更好”

async register(name, email, password,type) {
    let id;
    const createUser = this.functions.httpsCallable('createUser');

    const newUser = await this.auth.createUserWithEmailAndPassword({email,password });
    id = newUser.user.uid;
    // assuming the next two functions are asynchrnous AND return a promise
    // if not, just remove await
    await newUser.user.updateProfile({displayName: name});
    await createUser({
        id:id,
        name:name,
        email:email,
        type:type
    });
}

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

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