6

I have the following code :

var ORM = require('../helpers/mysql_orm');
var log = require('../helpers/logger');  

function UserModel() {

   this.User = ORM.define('User', {

        }, {
        tableName: 'User',
        timestamps: false,
        }); 
}


UserModel.prototype.findOneByCredinitals = function (creditinals) {

    this.User.findOne({ 
            attributes:['username','id'],
            where: 
             { 
              username:creditinals.principal,
              password:creditinals.creditinal}
            })
            .then(function(user) {
           console.log("inside then function"+user);
            return user;
    })
   // console.log(result);

},
UserModel.prototype.findAllByLimit = function(req,res) {


}


module.exports= new UserModel;

//app.js

var result = User.findOneByCredinitals({principal: 'admin',creditinal:'danieladenew'});

console.log("returned "+result);

This the result from app.js

------------------------------------------------
returned undefined
inside then function User Sequelize Instance i.e Useriffound and displayed.

How do i return user object from then to app.js code please ?

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Oct 03 '16 at 09:22

1 Answers1

2

You need to return the promise in order for the data to appear.

UserModel.prototype.findOneByCredential = function (credentials) {

    return this.User.findOne({ 
        attributes: ['username', 'id'],
        where: { 
            username: credential.principal,
            password: credential.credential
        }
    }).then(function(user) {
            console.log("inside then function"+user);
            return user;
        }
    });
},

Also, it might be a good idea to add a catch block. If the query fails, you will have no idea otherwise

ozanmuyes
  • 721
  • 12
  • 26
Tikkes
  • 4,599
  • 4
  • 36
  • 62
  • how to access data from the promise? the actull data is not stil seen on app.js – Develop4Life Oct 03 '16 at 07:20
  • What exactly do you mean? Normally whatever is returned by the promise, in this case `user` should be accessible in the resultset. so your `var result` should contain `user` object if done properly. Does the console.log in your `UserModel` print out the `user`? – Tikkes Oct 03 '16 at 07:22
  • `user` object is undefined `console.log(result.user) ` . Thanks – Develop4Life Oct 03 '16 at 07:34
  • it won't be `result.user` but `result` in stead. – Tikkes Oct 03 '16 at 07:39
  • Yes Sir. `result` on console shows/returns `Promise {...diffrent attributes}` just like this `console.log(result) ` – Develop4Life Oct 03 '16 at 07:41
  • It strikes me as odd also that you are using `User.findOneBy....` while the function is added to `UserModel`. I also do believe your `module.exports` should return an instance of `UserModel`, i.e. `module.exports = new UserModel()` (note the brackets) – Tikkes Oct 03 '16 at 07:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124768/discussion-between-danielad-and-tikkes). – Develop4Life Oct 03 '16 at 08:03