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

查看:22
本文介绍了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:

错误错误:未捕获(承诺):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 会导致它访问该属性,但事实并非如此.您可以看到我在整个代码中散布了一些控制台日志功能.当我运行我的函数时,返回空值"被返回,

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() 调用了无效数据.不支持的字段值:未定义(在字段角色中找到)

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

告诉您您为 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天全站免登陆