Nodejs + Mongoose的回调函数,如何只返回搜索值?

时间:2017-01-24 13:30:49

标签: node.js mongodb mongoose mongodb-query

我已经研究过如何做到这一点,但我仍然无法理解,我哪里出错了? 我研究了如何做这种类型的函数,但是我无法理解如何在回调中得到答案,总是在另一个函数中有2个函数。

  

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
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

    }


}

2 个答案:

答案 0 :(得分:0)

var resp = isUserSearch(email,function(data){
  return data;
  console.log(data); // I get my data 
});

console.log("Response : " + resp); // undefined
由于非阻塞异步架构 node.js提供,

resp未定义。

当返回数据isUserSearch的函数尚未完成时,将执行您尝试记录该值的行。

您是否已尝试在控制器中呼叫isUserSearch而不是isUser

var login = function(req,res){
    var data = req.body;
    var email = data.email;
    var password = Crypto.cryptoString(data.password);

    User.isUser(email, function(existUser) {
       if(existUser){
          console.log('User exist', 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
       } else {
          console.log('User does not exist');
       }
    });
}

然后您可以删除isUser并更改:

var result = {
    gerarToken : gerarToken,
    isUser: isUserSearch,
}

答案 1 :(得分:0)

您只需要带回调的isUserSearch功能。使用AuthCtrl中回调中返回的数据,您将其称为:

Controller UserCtrl

// Models
var User = require('../models/user');

var isUserSearch = function(email, callback){
    /* use findOne() which returns a single document that you can check if null */
    User.findOne({email: email}, function(err, user){ 
        if (err) throw err;
        return callback(!!user); // return a callback with a Boolean value as argument
    });
}

var result = {
    gerarToken : gerarToken,
    isUser: isUserSearch,
}

module.exports = result;

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);

    /* Call the isUser function with a callback */
    User.isUser(email, function(userExists){
        if (userExists) {
            // userExists is a boolean value
        }
    });

}