user172902
user172902

Reputation: 3601

Get path for in firebase after pushing onto a list

I am saving some user information into the firebase database. I also want to retrieve the path of it after it has been saved successfully. I am able to do it fine, however, I am confused about the data that I got back as shown below.

screenshot

So if I want to get the path, I have to run

data.path.o

Why is this format so weird? I see U, Nc, m, path, Y o. Are they suppose to look like that or am I missing something?

I am using angular 2 + angularfire2 with the following code

  saveUserToFirebase(user: User): firebase.database.ThenableReference {
    const userRef = this.af.database.list('/users/' + user.company);
    return userRef.push(user)
  }

this.databaseService.saveUserToFirebase(user)
    .then( data => {
        console.log('User saved to firebase databse with path', data); 
        console.log('User saved to firebase databse with path1', data.path); 
        console.log('User saved to firebase databse with path2', data.path.o); 

        this.router.navigate(['/login']);
        this.isLoading = false;
    }, error => {
        this.isLoading = false;
        console.log("Error asjf0e", error);
    })

Upvotes: 1

Views: 834

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599856

What you're returning from saveUserToFirebase is a ThenableReference, which is reference to a location in the database. A reference doesn't contain the actual data, it is just a pointed to the location.

To get the full URL from a reference, call toString on it:

this.databaseService.saveUserToFirebase(user)
    .then( ref => {
        console.log('User saved to firebase database with URL', ref.toString()); 

        this.router.navigate(['/login']);
        this.isLoading = false;
    }, error => {
        this.isLoading = false;
        console.log("Error asjf0e", error);
    })

To get just the absolute path (without the domain), you can use string manipulation:

var path = ref.toString().substring(ref.root.toString().length);

Upvotes: 8

Related Questions