0

I have a function which has a sequelize query

getsuperadmin = function(){
var user = User.findOne({
    where: { roleid: 1 },
    attributes:['userid','username','email']
}).then(user => {

});
return user;

}

I am trying to call this function getsuperadmin in another function in the same js file and console.log as below

 getadmindetails(){
    console.log(getsuperadmin());
 }

I am getting the below output in my console

 Promise [Object] {
 _bitField: 0,
 _fulfillmentHandler0: undefined,
 _rejectionHandler0: undefined,
 _promise0: undefined,
 _receiver0: undefined }

Why am I getting this output instead of the actual result

Wyck
  • 10,311
  • 6
  • 39
  • 60
rji rji
  • 697
  • 3
  • 17
  • 37
  • because you're returning a promise? – mehulmpt Jul 24 '19 at 13:36
  • @mehulmpt Yes, how can I get data in promise function? – rji rji Jul 24 '19 at 13:38
  • 1
    _"Why am I getting this output instead of the actual result"_ -- this is the actual result. Once you start an asynchronous operation (`User.findOne()` f.e.) you get a `Promise` back and the code continues without waiting for the async operation to complete (this means "asynchronous", after all). The result you expect is passed as argument to the function you pass to `.then()`. – axiac Jul 24 '19 at 13:53

4 Answers4

2

You need to catch the result of your promise in getadmindetails, either using async/await, or .then.

async function getadmindetails(){
    console.log(await getsuperadmin());
 }

or

function getadmindetails(){
    getsuperadmin.then(console.log);
}

As a side note, your getsuperadmin can just return the result of User.findOne in this case.

tpyle
  • 47
  • 9
1

You probably want to do something like:

 async function getadmindetails() {
    console.log(await getsuperadmin());
 }

Which waits for your promise to resolve

and for your DB call above, this is enough:

function getsuperadmin() {
var user = User.findOne({
    where: { roleid: 1 },
    attributes:['userid','username','email']
})
return user;
}

Read more about promises here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

mehulmpt
  • 15,861
  • 12
  • 48
  • 88
1
   getsuperadmin = function(){
       return User.findOne({
                 where: { roleid: 1 },
                 attributes:['userid','username','email']
       });
    }


    getadmindetails(){
       getsuperadmin.then(user => {
          console.log(user);
       })
    }
Ashish Mehta
  • 7,226
  • 4
  • 25
  • 52
0

You have the right idea, but your code has a few issues. You really just want to wrap the asynchronous part of your code in a Promise. You can then call .then on the resolved promise:

const getsuperadmin = function() {
  console.log('Fetching user data from server..');

  //Wrap your async code in a Promise and return the Promise
  return new Promise((resolve, reject) => {
    //Here I used setTimeout, but you would replace this bit with your code..
    setTimeout(() => resolve(JSON.stringify({
      userid: 1,
      username: 'John Doe',
      email: 'john.doe@-example-.com'
    })), 1500);
  })
}

//Call then to run some callback code once your async code completes
getsuperadmin().then(user => {
  console.log(JSON.parse(user));
})
Tom O.
  • 5,730
  • 2
  • 21
  • 35