【问题标题】:FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field nombre)FirebaseError:使用无效数据调用函数 DocumentReference.set()。不支持的字段值:未定义(在字段名称中找到)
【发布时间】:2020-04-24 12:32:54
【问题描述】:

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

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

代码:

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


}

【问题讨论】:

  • 我曾经尝试从非表单对象中读取表单数据时遇到过类似的问题。

标签: angular typescript firebase google-cloud-firestore


【解决方案1】:

错误消息告诉您,在您尝试将其添加到 Firestore 文档时,名为“nombre”的字段的 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. 或从您要添加的对象中删除它

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2019-04-25
    • 2019-12-20
    • 2019-03-08
    • 2018-08-11
    • 2021-12-26
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多