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