Google Firebase Angular Firestore switchMap-Firebase错误DocumentReference.set() [英] Google Firebase Angular Firestore switchMap - Firebase Error DocumentReference.set()

查看:91
本文介绍了Google Firebase Angular Firestore switchMap-Firebase错误DocumentReference.set()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置Angular Firebase应用程序,我要与该应用程序共享的用户数据存储在Firestore数据库中.我收到以下错误:

I'm trying to setup an Angular Firebase app where the user data that I want to share with the App is stored in the Firestore Database. I'm getting the following error:

ERROR错误:未捕获(承诺):FirebaseError:[code = invalid-argument]:调用函数DocumentReference.set()无效数据.不支持的字段值:未定义(在字段角色中找到)

ERROR Error: Uncaught (in promise): FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field role)

我的数据设置如下:

我的用户服务设置如下:auth.service.ts

My user service is setup like this: auth.service.ts

import { Injectable } from "@angular/core";
import { Router } from '@angular/router';

import { auth } from 'firebase/app';
import {
    AngularFirestore,
    AngularFirestoreDocument
} from '@angular/fire/firestore'

import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { User } from './user.model';

import * as firebase from 'firebase';
import { AngularFireAuth } from '@angular/fire/auth';
import { faGameConsoleHandheld } from '@fortawesome/pro-light-svg-icons';

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

  user$: Observable<User>;

  constructor(private afAuth: AngularFireAuth,
              private afs: AngularFirestore,
              private router: Router) {
        //This is how we're getting into the firestoreDB        
        this.user$ = this.afAuth.authState.pipe(
          switchMap(user => {
            if (user){
              console.log("user from constructor", user)
              return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
            } else {
                console.log("returning null")
                return of(null)
            }
          })
        )
              } //end constructor

  private updateUserData(user){
    //sets user data to firestore on login
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`)
    console.log("userRef", userRef)
    const data = {
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      role: user.role,
      thursdayCampaign: user.thursdayCampaign,
      menagerieCoast: user.menagerieCoast
    }
    console.log("role:", user.thursdayCampaign)
    return userRef.set(data, { merge: true})
  }

  async emailSignin(value){
    const provider = new firebase.auth.EmailAuthProvider();
    const credential = await this.afAuth.signInWithEmailAndPassword(value.email, value.password)
    return this.updateUserData(credential.user)
  }

user.model.ts:

user.model.ts:

export interface User {
  uid: string;
  email: string;
  displayName?: string;
  role: string;
  thursdayCampaign: Boolean;
  menagerieCoast: Boolean;

}

我已经缩小了创建错误的问题,该应用程序正在从构造函数中的this.user $返回null,但是我不确定为什么要这样做.我以为设置AngularFirestoreDocument会导致它访问该属性,但事实并非如此.您可以看到我在整个代码中散布了一些控制台日志功能.当我运行函数时,将返回返回null"

I've narrowed down the problem that is creating the error, the app is returning null from this.user$ in the constructor, but i'm not sure why it's doing that. I thought that setting AngularFirestoreDocument would cause it to access that property but it is not. You can see I have some console log functions sprinkled throughout the code. The "returning null" is getting returned when I run my function,

推荐答案

错误消息(强调我的问题):

The error message (emphasis mine):

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

告诉您您为 role 字段传递了一个未定义的值.这意味着您在 user.role 中有一个未定义的值.我们看不到这里的原因,因此您必须通过找出该 user 对象的来源来对其进行调试.

is telling you that you passed an undefined value for the role field. That means you have an undefined value in user.role. We can't see why that is here, so you'll have to debug it by finding out where that user object comes from.

这篇关于Google Firebase Angular Firestore switchMap-Firebase错误DocumentReference.set()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆