FirebaseError:使用无效数据调用函数 DocumentReference.set()。不支持的字段值:未定义(在字段名称中找到)

And*_*res 5 firebase typescript angular google-cloud-firestore

以下代码可用于在 Firebase 中创建新用户,但在浏览器中出现以下错误,您能帮我理解为什么吗?

使用无效数据调用函数 DocumentReference.set()。不支持的字段值:未定义(在字段名称中找到)

r

代码:

export class AuthService {

  private userSubscription: Subscription = new Subscription();

  constructor(private afAuth: AngularFireAuth,
    private router: Router,
    private afDB: AngularFirestore,
    private store: Store<AppState>) { }

initAuthListener() {

    this.afAuth.authState.subscribe((fbUser: firebase.User) => {
      if (fbUser) {
        this.userSubscription = this.afDB.doc(`${fbUser.uid}/usuario`).valueChanges()
          .subscribe((usuarioObj: any) => {

            const newUser = new User(usuarioObj)

            this.store.dispatch(new SetUserAction(newUser))

          })
      } else {
        this.userSubscription.unsubscribe()
      }
    })
  }

  createUser(nombre: string, email: string, password: string) {

    this.store.dispatch(new ActivarLoadingAction())

    this.afAuth.auth
      .createUserWithEmailAndPassword(email, password)
      .then(resp => {


        const user: User = {
          uid: resp.user.uid,
          nombre: nombre,
          email: resp.user.email
        }

        this.afDB.doc(`${user.uid}/usuario`)
          .set(user)
          .then(() => {

            this.router.navigate(['/'])
            this.store.dispatch(new DesactivarLoadingAction())
          })


      })
      .catch(erro => {
        console.error(erro)
        this.store.dispatch(new DesactivarLoadingAction())
        Swal.fire('Error!', erro.message, 'error')
      })
  }


}

Run Code Online (Sandbox Code Playgroud)

Dou*_*son 5

该错误消息告诉您,名为“nombre”的字段undefined在您尝试将其添加到 Firestore 文档时具有 JavaScript 值。 undefined不是 Firestore 文档字段值的有效值。

该错误特指此代码:

    const user: User = {
      uid: resp.user.uid,
      nombre: nombre,
      email: resp.user.email
    }

    this.afDB.doc(`${user.uid}/usuario`)
      .set(user)
Run Code Online (Sandbox Code Playgroud)

要解决此问题,您应该确保:

  1. nombre 有一个非未定义的值
  2. 或从您添加的对象中删除它