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

Posted

技术标签:

【中文标题】Angular Firebase 错误 ---- 使用无效数据调用的函数 DocumentReference.set()。不支持的字段值:未定义(在字段姓氏中找到)【英文标题】:Angular Firebase Error ---- Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field lastname) 【发布时间】:2020-03-21 09:53:01 【问题描述】:

如果我注释掉“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();
  

【问题讨论】:

【参考方案1】:

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

【讨论】:

啊。好的。我没有注意表单字段中的区分大小写。特别是输入中的“id”……叹息……感谢同行评审。

以上是关于Angular Firebase 错误 ---- 使用无效数据调用的函数 DocumentReference.set()。不支持的字段值:未定义(在字段姓氏中找到)的主要内容,如果未能解决你的问题,请参考以下文章

PayPal Rest API、firebase 函数和 Angular 6 出现 CORS 错误

node_modules/@angular/fire/firebase.app.module.d.ts 中的错误?

当前未初始化任何 firebase.app.App 实例。 Angular 6 登录时出现电话错误

为啥只有在部署到 Firebase 时才会在 Node.js 和 Angular 之间出现 CORS 错误?

为啥我在尝试从 Angular 2 应用程序连接和查询 Firebase 数据库时收到此错误消息?

迁移 Angular Universal firebase 后,云函数抛出错误