Note that there are some explanatory texts on larger screens.

plurals
  1. PONode function finishing before Mongoose db find method
    primarykey
    data
    text
    <p>Why could the example below be returning the node middle-ware function before my <em>mongoose</em> db lookup function finishes? I am sure this is a async issue but I am a bit lost on why. </p> <p><strong>Middle-ware js file</strong></p> <pre><code>var mongoose = require('mongoose'), Schema = mongoose.Schema; function isUser(login){ var UsersSchema = new Schema({ user: String, user_type: String, password: String, first_name: String, middle_name:String, last_name: String, birth_date: Date, join_date: Date }); var UserModel = mongoose.model('users', UsersSchema); mongoose.connect('mongodb://localhost/identity'); mongoose.model('users', UsersSchema); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error: ')); db.once('open', function cb () { UserModel.findOne({'user': login}, function (err, user){ if (err){ throw err; } console.log('connected to user db and preformed lookup.'); console.log(user); return user; }); }); } module.exports.authenticate = function (login, password, cb) { var user = isUser(login), msg; console.log(user); if (!user) { msg = 'Invalid User'; cb(null, msg); return; } if (user.password != password) { msg = 'Invalid Password'; cb(null, msg); return; } cb(user, null); }; </code></pre> <p><strong>Console Output</strong></p> <pre><code>undefined Login Failed! : Invalid User connected to user db and preformed lookup. { _id: 51c8e16ce295c5b71ac6b229, user: 'bclark@themindspot.com', user_type: 'admin_master', password: 'enter', first_name: 'Brandon', middle_name: 'Laurence', last_name: 'Clark', birth_date: Fri Mar 19 1982 00:00:00 GMT-0800 (PDT), join_date: Wed Jun 26 2013 00:00:00 GMT-0700 (PDT) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload