Estoy trabajando en una aplicación y quiero lograr esto. Cuando un usuario inicia sesión, quiero obtener su nombre de usuario y almacenarlo en firebase. Así que creé esto:
Signup(email: string, password: string, displayName: string) { this.angFire.auth.createUser({ email: email, password: password }).catch( (err) => { console.log(err); this.error = err; }) ...Y esto funciona, ahora necesito guardar también displayName en el usuario actual, así que trato de hacer esto dentro de la misma función (Singup()) :
let user = firebase.auth().currentUser; user.updateProfile({ displayName: this.displayName, photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(function() { console.log("Nome utente inserito"); }, function(error) { console.log("Errore durante inserimento utente"); });Pero me da esto:
error_handler.js:54 EXCEPTION: Error in ./SignupPage class SignupPage - inline template:70:4 caused by: Cannot read property 'updateProfile' of null¿Cómo hacerlo? Estoy corriendo en ionic 2 angular2, firebase/angulrfire
Referencia del documento:
Actualizar el perfil de un usuario
Puede actualizar la información de perfil básica de un usuario (el nombre para mostrar del usuario y la URL de la foto de perfil) con el método updateProfile. Por ejemplo:
var usuario = firebase.auth().currentUser;
user.updateProfile({ displayName: "Jane Q. User", photoURL: " https://example.com/jane-q-user/profile.jpg " }).then(function() {
// Actualización exitosa. }, function(error) { // Ocurrió un error. });
Mi respuesta es demasiado tarde, pero en caso de que alguien esté interesado, podría hacerlo así.
constructor( private afAuth: AngularFireAuth, private router: Router ) { // } async signupUser(email: string, password: string): Promise<any> { const data = await this.afAuth.createUserWithEmailAndPassword( email, password ); return this.updateUserData(data.user).then(() => { this.router.navigate(["/"]); }); } private updateUserData({ uid, email, photoURL, displayName }: User) { const userRef: AngularFirestoreDocument<User> = this.afs.doc( `users/${uid}` ); return userRef.set({ uid, email, photoURL, displayName }, { merge: true }); }Puedes aprender más aquí