I already researched how to do this, but I still can not understand, where am I going wrong ? I researched how to do this type of function but I could not understand how to get an answer in my callback, always end up having 2 functions within the other.
Controller UserCtrl
// Models
var User = require('../models/user');
var isUserSearch = function(email,callback){
User.find({email:email},function(err,data){
if(err) throw err;
return callback(data);
});
}
var isUser = function(email){
var resp = isUserSearch(email,function(data){
return data;
console.log(data); // I get my data
});
console.log("Response : " + resp); // undefined
return resp;
}
var result = {
gerarToken : gerarToken,
isUser : isUser,
}
module.exports = result;
Model
// Model
var mongoose = require('mongoose');
// Schema
var Schema = mongoose.Schema({
name : {
type : String,
require : true
},
email : {
type : String,
require : true,
unique : true
},
password : {
type : String,
required : true
},
type : {
type : Number,
required : true,
default : 1
},
created : {
type : Date,
default : Date.now
}
});
var Data = mongoose.model('User', Schema);
module.exports = Data;
Context AuthCtrl
// Controllers
var Crypto = require('./cryptoCtrl');
var User = require('./UserCtrl');
// ----------- Login
var login = function(req,res){
var data = req.body;
var email = data.email;
var password = Crypto.cryptoString(data.password);
var existUser = User.isUser(email);
if(existUser){
// IsUser is a function to return the user array
// if it exists, otherwise it returns only a false
// boolean value. In the example I'm going to use this function
}
}