使用无效数据获取错误DocumentReference.set()。在Firestore中设置用户数据时

时间:2018-06-27 18:43:58

标签: angular firebase google-cloud-firestore

所以我正在尝试将用户数据从Firebase保存到Firestore中,并且不断收到此错误

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

这里涉及2种方法:

  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)则说未定义。有人可以帮我弄清楚为什么会这样吗?

1 个答案:

答案 0 :(得分: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));
  }