简体   繁体   English

Module.exports通过Mongoose调用返回未定义的

[英]Module.exports returns undefined with Mongoose call

I keep getting an undefined return on my find call with Mongoose. 在Mongoose的find呼叫中,我一直得到undefined回报。 My result in my exports file doesn't get logged, but it will work if I return a simple string outside my Projects.find call. 我的导出文件中的结果没有被记录,但是如果我在Projects.find调用之外返回一个简单的字符串,该结果将起作用。

I'm passing req & res and they are logged correctly in my exports file, so don't think they have anything to do with the problem. 我通过了reqres ,它们已正确记录在我的导出文件中,所以不要认为它们与问题有关。 Any ideas what's going wrong? 任何想法出什么事了吗?

routes.js routes.js

var proj = require('./exports/projects');

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});

exports/projects.js exports / projects.js

var Projects = require('../models/projects');

module.exports = {
    test: function(req, res) {
        Projects.find({'owner':req.user.id}, function(err, result) {
                if (err) return '1';
                if (!result)
                    return null;
                else
                    return result;
        });
    }
};

models/projects.js models / projects.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var shortid = require('shortid');

var Projects = new Schema({
        projectid: String,
        pname: { type: String, required: true, trim: true },
        owner: { type: String, required: true, trim: true },
        status: { type: String, default: '0' },
        team: String,
        archived: { type: Boolean, default: '0' },
        created_at: Date
});

Projects.pre('save', function(next) {
    var currentDate = new Date();
    this.created_at = currentDate;
    this.projectid = shortid.generate();
    next();
});

module.exports = mongoose.model('Projects', Projects);

It is due to asynchronous nature of the Project.find() method. 这是由于Project.find()方法的异步性质。 You are trying to return a value in asynchronous function, which gets completed after some time. 您正在尝试在异步函数中返回一个值,该值将在一段时间后完成。 Thus is gets undefined value in return while executing proj.test(req, res) in console.log('response: ' + proj.test(req, res)); 因此proj.test(req, res)console.log('response: ' + proj.test(req, res));执行proj.test(req, res) ,返回的是未定义的值console.log('response: ' + proj.test(req, res)); .

Solution Need to pass a callback function, which gets executed once the find operation is done. 解决方案需要传递一个回调函数,一旦完成find操作,该函数便会执行。

routes.js routes.js

app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});

exports/projects.js exports / projects.js

module.exports = {
    test: function(req, res, cb) {
        Projects.find({'owner':req.user.id}, function(err, result) {
                if (err) return cb(1);
                if (!result)
                    return cb(null);
                else
                    return cb(result);
        });
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM