0

Using NodeJS and Mongoose I'm trying to create a database file that allows me to query the database and return the result to be able to work with without using Mongoose.

In my database file I have this

db.js

module.exports = {

  getUser: function (name) {
    var u = User.findOne({username: name}).exec().then(function(user) {
      console.log(user);
      return user;
    });
    return u;
  }

};

and in my other file calling it I have

var db = require('./db')

var user_info = db.getUser("tom");
console.log(user_info);

When the getUser function is called it correctly prints the information I'm looking for in the db.js file with the following line of code.

console.log(user);

However I can't seem to find a way of getting the database to return the value into user_info, I just get

Promise { <pending> }

I've looked into .then and callbacks but can't seem to figure out how to get it to work I just always end up in an endless cycle of Promise pending.

Thanks

w13rfed
  • 355
  • 5
  • 12

3 Answers3

2
var db = require('./db')

var user_info = db.getUser("tom");
console.log(user_info);

user_info.then(user => {
    const username = user.username;
    console.log(username);
});

Promises execute. But, you get access to the result on after a .then.

Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51
  • Brilliant thanks, if there is a field in user called username, is there a way of getting that string out of the user_info and assigning it to something else, something like `var username = user_info.then(user => { return user.username; });` ? – w13rfed Feb 22 '18 at 15:21
  • 1
    Yeah. You can totally do that. But like this: `user_info.then(user => { var username = user.username; console.log(username); });` – Faizuddin Mohammed Feb 22 '18 at 16:12
  • No I mean if you wanted to use username outside of the `user_info.then()` function, so before `user_info.then()` do `var username;` then after the `user_info.then()` use username in something else? – w13rfed Feb 22 '18 at 16:17
  • 1
    Not possible with Promises. JS is async, so, you'll need to know when the `db.getUser` operation completed. And only then can you use the results. So, `.then` acts like the place where you can be sure that the `user` variable is available. – Faizuddin Mohammed Feb 22 '18 at 16:23
  • You might also want to look into `await async` but I would strongly suggest to get thorough with promises before you go there. – Faizuddin Mohammed Feb 22 '18 at 16:24
  • I'm trying to create a webapp that queries a database then formats information from the database to display on a webpage and do analysis on the data, is this possible with promises or would the `await async` version be more suitable? – w13rfed Feb 22 '18 at 16:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165659/discussion-between-faizuddin-mohammed-and-w22). – Faizuddin Mohammed Feb 22 '18 at 16:37
  • @w13rfed Can you share your result? Even I require the solution as you needed – program_bumble_bee May 21 '20 at 19:56
1

you must call .then on the promise to capture the results:

check here Promise pending

Rupesh
  • 850
  • 2
  • 13
  • 30
1

You need to call .then for getUser.

// db.js
module.exports = {
    getUser: function (name) {
        return User.findOne({
            username: name
        }).exec();
    }
};


var db = require('./db');
db.getUser("tom").then((user) => {
    console.log(user);
}).catch((err) => {
    console.log(err);
});
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37