Angular Firebase错误-—函数DocumentReference.set()被无效数据调用。不支持的字段值:未定义(在字段姓氏中找到)

问题描述 投票:1回答:1

如果我注释掉“ lastname:this.newUser.lastName”,错误消失(从底部开始9行)

我真的很迷住这里。我查看了所有的拼写等内容,但似乎仍然可以找出发生这种情况的原因。

话虽这么说,有没有更简单的方法来调试角度类型脚本?我对这个宇宙还很陌生。

    import { Injectable } from '@angular/core';
    import { AngularFireAuth } from '@angular/fire/auth';
    import { AngularFirestore } from '@angular/fire/firestore';
    import { Router } from '@angular/router';
    import { ThrowStmt } from '@angular/compiler';
    import { BehaviorSubject } from 'rxjs';

    @Injectable({
      providedIn: 'root'
    })
    export class AuthService {

      private eventAuthError = new BehaviorSubject<string>("");
      eventAuthError$ = this.eventAuthError.asObservable();

      newUser: any;

      constructor(
       private afAuth: AngularFireAuth,
       private db: AngularFirestore,
       private router: Router) { }

      getUserState() {
        return this.afAuth.authState;
      }

login( email: string, password: string) {
 this.afAuth.auth.signInWithEmailAndPassword(email, password)
  .catch(error => {
    this.eventAuthError.next(error);
  })
  .then(userCredential => {
    if(userCredential) {
      this.router.navigate(['/home']);
    }
  })
}

createUser(user) {
console.log(user);
this.afAuth.auth.createUserWithEmailAndPassword( user.email, user.password)
  .then( userCredential => {
    this.newUser = user;
    console.log(userCredential);
    userCredential.user.updateProfile( {
      displayName: user.firstName + ' ' + user.lastName
    });

    this.insertUserData(userCredential)
      .then(() => {
        this.router.navigate(['/home']);
      });
  })
  .catch( error => {
    this.eventAuthError.next(error);
  });
 }

 insertUserData(userCredential: firebase.auth.UserCredential) {
  return this.db.doc(`Users/${userCredential.user.uid}`).set({
   email: this.newUser.email,
   firstname: this.newUser.firstName,
   lastname: this.newUser.lastName,
   role: 'network user'
   })
  }

  logout() {
    return this.afAuth.auth.signOut();
  }
}
javascript angular firebase google-cloud-firestore
1个回答
0
投票

该错误消息告诉您,您正在将JavaScript值undefined作为文档中称为“姓氏”的属性进行传递。 Firestore不接受未定义的值。您将不得不为姓氏分配一些其他值。如果您打算始终有一个姓氏值,则应在尝试将其写入文档字段或将其完全排除在文档之外之前检查其有效性。

© www.soinside.com 2019 - 2024. All rights reserved.