【发布时间】:2017-01-24 22:16:20
【问题描述】:
我已经研究过如何做到这一点,但我仍然无法理解,我哪里出错了? 我研究了如何执行这种类型的函数,但我不明白如何在我的回调中得到答案,最终总是在另一个函数中有 2 个函数。
控制器用户Ctrl
// 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
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;
上下文 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
}
}
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query