【问题标题】:Getting error DocumentReference.set() called with invalid data. when setting user data in Firestore使用无效数据调用错误 DocumentReference.set()。在 Firestore 中设置用户数据时
【发布时间】:2018-06-27 18:43:57
【问题描述】:

所以我试图将用户数据从 firebase 保存到 firestore,但我不断收到此错误

DocumentReference.set() called with invalid data. Unsupported field 
value: undefined (found in field uid)

这里有两种方法:

  register(credentials) {
    return this.afAuth.auth.createUserWithEmailAndPassword(credentials.email, credentials.password).then(user => {
      return this.setUserDoc(user);
    }).catch(error => console.log(error.message));
  }

  private setUserDoc(user) {
    console.log(user);
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
    const data: User = {
      uid: user.uid, <-- says this is undefined
      email: user.email
    }

    return userRef.set(data);
  }

如果我 console.log(user) 被传递到 setUserDoc 方法中,我可以打开它并且它有一个 uid 但如果我 console.log(user.uid) 它说未定义。有人可以帮我弄清楚为什么会这样吗?

【问题讨论】:

    标签: angular firebase google-cloud-firestore


    【解决方案1】:

    好的,我想通了。我只是太高了一级。如果我将呼叫更改为通过credential.user,它就起作用了。所以现在的代码是:

    register(credentials) {
        return this.afAuth.auth.createUserWithEmailAndPassword(credentials.email, credentials.password).then(credential => {
          return this.setUserDoc(credential.user); <-- changed what was passed
        }).catch(error => alert(error.message));
      }
    

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 2021-08-29
      • 2020-08-20
      • 2020-06-13
      • 2021-02-24
      • 2019-03-08
      • 2018-08-11
      • 2021-02-26
      相关资源
      最近更新 更多