Firebase错误:调用DocumentReference.set()函数时使用了无效数据。不支持的字段值:undefined(在字段nombre中找到)。

6

以下代码可以在 Firebase 中创建新用户,但是在浏览器中出现以下错误,请帮助我理解其中的原因:

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

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')
      })
  }


}


我曾经遇到过类似的问题,尝试从非表单对象中读取表单数据。 - Daniel Danielecki
1个回答

6

错误信息告诉您,在您试图将"nombre"字段添加到Firestore文档时,它的JavaScript值为undefinedundefined不是Firestore文档字段值的有效值。

该错误特别指向了这段代码:

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

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

为了解决这个问题,您需要确保以下之一:
  1. nombre有一个不是未定义的值
  2. 或将其从要添加的对象中移除

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接