gpt4 book ai didi

javascript - Angular Firebase 错误 ---- 使用无效数据调用函数 DocumentReference.set()。不支持的字段值 : undefined (found in field lastname)

转载 作者:行者123 更新时间:2023-11-28 03:22:08 25 4
gpt4 key购买 nike

如果我注释掉“lastname: this.newUser.lastName,”(从底部向上 9 行),错误就会消失

我真的被难住了。我检查了所有拼写等,似乎仍然可以找出发生这种情况的原因。

话虽如此,是否有更简单的方法来调试 Angular 类型脚本?我对这个宇宙还很陌生。

    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();
}
}

最佳答案

该错误消息告诉您,您正在将 JavaScript 值 undefined 作为文档中名为“lastname”的属性传递。 Firestore 不接受未定义的值。您必须为姓氏指定其他值。如果您希望始终存在姓氏值,则应在尝试将其写入文档字段之前检查其有效性,或者将其完全保留在文档之外。

关于javascript - Angular Firebase 错误 ---- 使用无效数据调用函数 DocumentReference.set()。不支持的字段值 : undefined (found in field lastname),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59043132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com