Node function finishing before Mongoose db find method
db.once and UserModel.findOne are asynchronous functions, hence why you provide an anonymous function that is called when they are done. If you want your isUser function to 'return' the results of these asynchronous functions you will have to make it use a callback as well.
Replacefunction isUser(login){
withfunction isUser(login, callback){
andreturn user;
withcallback(user)
.
It is also recommended to not throw errors in asynchronous code, but pass them on with the callback, similar to what db.once and UserModel.find do, like so:
Removeif (err){ throw err; }
and replace the callback above withcallback(err, user);
While you're at it, since you don't do anything with the error or user anymore, you might as well call UserModel.findOne({'user': login}, callback);
===
The complete thing would become the following. Note that I follow the callback(err, result) convention.
var mongoose = require('mongoose'), Schema = mongoose.Schema;function isUser(login, callback){ 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}, callback); });}module.exports.authenticate = function (login, password, cb) { var user = isUser(login, function(err, user) { if (err) { cb(err); } console.log(user); if (!user) { msg = 'Invalid User'; cb(msg); return; } if (user.password != password) { msg = 'Invalid Password'; cb(msg); return; } cb(null, user); });};
Finally, consider using (custom) Error objects instead of String messages, google it for explanations as to why.
yes this is a async issue. you may know that node.js every action has a separate thread
in your code you call
var user = isUser(login),
according to You it should return the result from the isUser
function but the execution give this function a separate thread and it goes on to the next statement.as on the next statement the user is undefined because it doesn't return anything from the function isUser
so the statement if (!user) {
becomes true
to avoid this error you should place isUser
in a callback function
means restrict the execution until and unless you get response from the function