Retrieve a value once from Firebase

Well, it is async by nature, so the consuming code needs to do a then, so it won’t be possible to have a function that returns username as pure string

And as the code is in screenshot, I cannot copy/paste easily. So if you can provide your code as text, I am happy to suggest.

What actually is the error code you are referring to?

Ok here is the function as text…

  getUsername()
  {
    // function goes to firebase and checks the profile node for the current users username 
    var userId = this.Auth.auth.currentUser.uid;

    return this.database.database.ref(`/profiles/${userId}/username/`).once('value').then(function(snapshot){
    var username = (snapshot.val() && snapshot.val().username) || 'Anoynymous';
   })
    
  }

Again, thanks for your help and patience

    getRemote(path) {
        // this.consoleLog('getRemote ', path);
        if (firebase.auth().currentUser != undefined)
            return firebase.database().ref(path).once('value')
                .then((snapshot) => {
                    return snapshot.val()
                })
        else return Promise.reject({ code: 'not-online-load-snapshot' })
    }

Ths is my code and potentially people will dislike the return Promise.reject I am doing to propagate any error.

Yours I would change…

 getUsername()
  {
    // function goes to firebase and checks the profile node for the current users username 
    let userId = this.Auth.auth.currentUser.uid; // not used, so skip???

    return this.database.database.ref(`/profiles/${userId}/username/`).once('value').then((snapshot) =>{
     return snapshot.val() || 'Anoynymous';
   })
    
  }

So the consumer would be like (assuming getUsername is located in a provider)

this.myprovider.getUsername()
.then(username=>{
 console.log('Yeah, username ', username);
})
.catch(error=>{
console.log('OOPS, error', error)
})

Don’t forget the CATCH, otherwise you will find yourself in error

1 Like

Thanks very much for this Tommertom, it is the furthest i’ve got with it so far :slight_smile:

One more question…

I have a function on one of my pages that takes user input from a form and saves the relevant data to Firebase.
It is with this data that I need to store the username… the function now looks like this…

  // Function that looks at the users input and posts the request details to database
  postRequestToFirebase(user: User)
  {
    // Take the inputs and save them into a Request object
    this.request.requestId = Math.random().toString(36).substr(2, 9);
    this.request.userId = this.Auth.auth.currentUser.uid;
    this.request.requestText = this.designType;
    this.request.bodyPosition = this.designBodyPosition;
    this.request.requestHashtags = this.designHashtags;
    this.request.requestDate = new Date().toLocaleString();    
    this.request.requestSolved = false;
    
    // Grab the username from Firebase
    this.request.username = this.data.getUsername().then(username=>{
      console.log('Yeah, username ', username);
     })
     .catch(error=>{
      console.log('OOPS, error', error)
      })

    // Log the request 
    console.log(this.request);

    // Create a node on our database under requests identified with a unique request ID  
    this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
    // Set the values of the node to our request inputs 
    this.requestObject.set(this.request);
  }

I want to store the username string to ‘this.request.username’ before sending to Firebase.
However the username is still not saving to Firebase along with the Request objects (probably because it is not a string?), however the 'console.log('Yeah, username ‘, username)’ is working fine. Is there anyway to save this username as a string into the request.username?

Thanks for your time

Anything you want happening once the username is received should go in the then where it says console.log('yeah')

Remember: it is async

this.request.username = this.data.getUsername().then(username=>{
      console.log('Yeah, username ', username);

 this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
    // Set the values of the node to our request inputs 
    this.requestObject.set(this.request);

..etc.

     })
     .catch(error=>{
      console.log('OOPS, error', error)


      })
  // Function that looks at the users input and posts the request details to database
  postRequestToFirebase(user: User)
  {
    // Take the inputs and save them into a Request object
    this.request.requestId = Math.random().toString(36).substr(2, 9);
    this.request.userId = this.Auth.auth.currentUser.uid;
    this.request.requestText = this.designType;
    this.request.bodyPosition = this.designBodyPosition;
    this.request.requestHashtags = this.designHashtags;
    this.request.requestDate = new Date().toLocaleString();    
    this.request.requestSolved = false;
    
    // Grab the username from Firebase
    this.data.getUsername().then(username=>{
      console.log('Yeah, username ', username);

      //this.request.username = username;
     })
     .catch(error=>{
      console.log('OOPS, error', error)
      })

    // Log the request 
    console.log(this.request.username);

    // Create a node on our database under requests identified with a unique request ID  
    this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
    // Set the values of the node to our request inputs 
    this.requestObject.set(this.request);
  }

As you can see, I am trying to fill the request object with the relevant data.
It seems that with my first ‘console.log(this.request.username)’ in the code I am getting a response of ‘undefined’…
But with the second 'console.log('Yeah, username ‘, username)’ I am getting the correct username (as shown below).

image

Because the request.username is undefined… it doesn’t seem to be posting to Firebase.

Please help, it is most likely because I havent set up an async function correctly.

Thanks

 // Function that looks at the users input and posts the request details to database
  postRequestToFirebase(user: User)
  {
    // Take the inputs and save them into a Request object
    this.request.requestId = Math.random().toString(36).substr(2, 9);
    this.request.userId = this.Auth.auth.currentUser.uid;
    this.request.requestText = this.designType;
    this.request.bodyPosition = this.designBodyPosition;
    this.request.requestHashtags = this.designHashtags;
    this.request.requestDate = new Date().toLocaleString();    
    this.request.requestSolved = false;
    
    // Grab the username from Firebase
    this.data.getUsername().then(username=>{
      console.log('Yeah, username ', username);

      this.request.username = username;

    // Log the request 
    console.log(this.request.username);

    // Create a node on our database under requests identified with a unique request ID  
    this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
    // Set the values of the node to our request inputs 
    this.requestObject.set(this.request);
     })
     .catch(error=>{
      console.log('OOPS, error', error)
      })


  }

as I said. All the stuff that is needed to be done once the value is obtained from an async call should go in the then.

All the code outside the then is called (or should be assumed to be) before the then is executed

I know what you mean, however I am getting an error with this part of the code…

// Grab the username from Firebase
    **this.request.username** = this.data.getUsername().then(username=>{
      console.log('Yeah, username ', username);

      this.request.username = username;
     })
     .catch(error=>{
      console.log('OOPS, error', error)
      })

Specifically, the error says… "Type ‘Promise’ is not assignable to type ‘string’ "

Thanks

But even when I specify ‘username’ as type ‘any’ on my interface like so…

// This interface dictates what a request will look like 
export interface Request 
{
    requestId: string;
    userId: string;
    username: any;
    userAvatar: string;
    requestText: string; 
    bodyPosition: string; 
    requestHashtags: string; 
    requestDate: string;
    requestSolved: boolean;
}

The request.username is still ‘undefined’.

Thanks

Well, that isn’t the code I provided you so…

fairly clear that promises don’t work that way…

let me quote again

the formatting sucks in this forum

  postRequestToFirebase(user: User) {
    // Take the inputs and save them into a Request object
    this.request.requestId = Math.random().toString(36).substr(2, 9);
    this.request.userId = this.Auth.auth.currentUser.uid;
    this.request.requestText = this.designType;
    this.request.bodyPosition = this.designBodyPosition;
    this.request.requestHashtags = this.designHashtags;
    this.request.requestDate = new Date().toLocaleString();
    this.request.requestSolved = false;

    // Grab the username from Firebase
    this.data.getUsername().then(username => {
      console.log('Yeah, username ', username);

      this.request.username = username;

      // Log the request 
      console.log(this.request.username);

      // Create a node on our database under requests identified with a unique request ID  
      this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
      // Set the values of the node to our request inputs 
      this.requestObject.set(this.request);
    })
      .catch(error => {
        console.log('OOPS, error', error)
      })
  }

Sorry I’m not 100% clear on Promises.

I will look them up and hopefully I can sort the problem then.

Thanks

Is really not the way to go with promises. The assignment to this.request.username must go in the then using the username variable as result from the promise resolution

(btw, there is a different way using await…async,but maybe you need to get this first!)

I can’t promise I can resolve that :wink:

1 Like

Thanks Tommertom,

I’m getting there slowly but surely :slight_smile:

Now when I ‘console.log(request.username)’ I am getting an object back (see below)… So I am just working to bind this value to a string or other value that can be exported and read by Firebase.

image

Many thanks for your time… most wouldn’t want to waste time on an extreme novice lol.

meaning you received an unresolved promise, you need to resolve using then