DocumentReference.set() 使用无效数据调用.不支持的字段值:未定义



我正在与Firebase进行此注册,并将用户详细信息存储在Firestore中。

我已经能够成功注册用户,但是当我将详细信息添加到数据库时,其中一个字段出现错误。 未定义。 当我尝试不使用catchPhrase字段时,它可以工作。

这是我的代码:auth.service.ts

emailSignUp(email: string, password: string) {
return this.afAuth.auth
.createUserWithEmailAndPassword(email, password)
.then(credential => {
this.customSetUSerData(credential.user);
})
.catch(error => {
console.log(error);
});
}
customSetUSerData(user: User) {
const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
const userData: User = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
catchPhrase: user.catchPhrase
}; // photoURL: user.photoURL,
return userRef.set(userData).catch(err => console.log(err));
}

这里是注册组件.html

ngOnInit() {
this.signUpForm = this.fb.group({
email: ['', [Validators.required, Validators.email]],
password: [
'',
[
Validators.pattern('^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$'),
Validators.minLength(6),
Validators.maxLength(25),
Validators.required
]
],
region: ['', []],
catchPhrase: ['', [Validators.required]],
photoUrl: []
});
}
get email() {
return this.signUpForm.get('email');
}
get password() {
return this.signUpForm.get('password');
}
get catchPhrase() {
return this.signUpForm.get('catchPhrase');
}
get photoUrl() {
return this.signUpForm.get('photoUrl');
}
signup() {
// console.log(this.email.value + this.password.value + this.catchPhrase.value);
return this.auth.emailSignUp(this.email.value, this.password.value);
}
}

这是错误:

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

错误消息非常明确:您正在尝试将字段catchPhrase设置为undefined,这不是一个有效的值。

这发生在以下代码中:

catchPhrase: user.catchPhrase

所以看来user.catchPhraseundefined.这是有道理的,因为 Firebase 身份验证用户没有catchPhrase属性。您可能希望将用户输入/选择的一些标语传递到您的代码中,在这种情况下,您应该将其作为单独的参数传递或像这样查找:

catchPhrase: catchPhrase

这将调用catchPhrase属性的(只读(getter:

get catchPhrase() {
return this.signUpForm.get('catchPhrase');
}

最新更新