0

Below is my code that I used to call a function that returns a value after fetching from the Firebase ref.

let original = Promise.resolve(this.contactService.fetchContact(contact));
    original.then(function(value){
      console.log("original value: " + value);//point 2
    });

the Firebase fetching function is as below:

fetchContact(name){
    //fetch contact object from name
    let contactObj = this.database.ref('path').orderByChild('name').equalTo(name).once('value').then(response=>{
        console.log(response.val());//Point 1
        return response.val();
    });
  }

The problem is, in the comment Point 1, I get the desired value as the output. But as I have returned the same to the Promise, I want it to be returned as the response in the comment Point 2. What I get is undefined in the comment Point 2

This is not the same as that of the Ajax call or Http Requests. This is within the local files. Where am I going wrong? Please help. Thanks!

gsthina
  • 1,090
  • 8
  • 22
  • Have you tried to `return contactObj;` in the `fetchContact` function? – Kmaschta May 31 '17 at 07:26
  • 1
    You don't need to wrap the result of `fetchContact` in yet another `Promise.resolve`. *If* you return the promise (`contactObj`) from `fetchContact`, a simple `fetchContact().then(...)` will do. – deceze May 31 '17 at 07:28
  • Thanks for the neat idea. It worked @deceze :) – gsthina May 31 '17 at 07:51

0 Answers0