【发布时间】: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