angularfirebasegeneral-network-error

New User not saved in Firebase using Angular


i am working in angular and want to add new user in Firebase, everything work fine without any error but when i open firebase Database it didn't show any data and show error in console

FIREBASE WARNING: set at /users/zJJNe15WyDW0TYh5L2khIp9BeH62 failed: permission_denied

after changing Rules allow read, write: if true; then error change to

Uncaught Error: Network Error

Empty Firebase Database

here is service through which i want to add new user

export class UserService {    
  constructor(private db : AngularFireDatabase) { }    
  save(user : firebase.User)
  {
    this.db.object('/users/' + user.uid).update({
      name : user.displayName,
      email : user.email
    });
  }
}

And here is imports of this service

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import * as firebase from 'firebase';

and this is app.component

export class AppComponent {
  constructor(private userService: UserService ,private auth : AuthService, private router :Router)
  {
    auth.user$.subscribe(user => {
      if (user)
      {
        userService.save(user);

        let returnUrl = localStorage.getItem('returnUrl');
        router.navigateByUrl(returnUrl);
      }
    });
  }
}

i use firebase for logging in which work perfectly and other functionality also work fine but here it is not saving new user in firebase.


Solution

  • You should use set to create a document - https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document

    You use update to update fields - https://firebase.google.com/docs/firestore/manage-data/add-data#update-data

    Try:

    save(user : firebase.User)
      {
        this.db.object('/users/' + user.uid).set({
          name : user.displayName,
          email : user.email
        });
      }
    

    Edit: It seems that you were experiencing "Uncaught Error: Network Error". This may be resolved by following this question - Uncaught Error: A network error