Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Module.exports返回未定义的Mongoose调用_Javascript_Node.js_Mongodb - Fatal编程技术网

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

Javascript Module.exports返回未定义的Mongoose调用,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,在我与猫鼬的find通话中,我不断收到undefined返回。我的成绩 在我的导出中,文件不会被记录,但如果我在我的项目之外返回一个简单字符串,它就会工作。find调用 我正在传递req和res,它们正确地记录在我的导出文件中,因此不认为它们与问题有关。你知道怎么回事吗 routes.js var proj = require('./exports/projects'); app.use(function(req, res, next){ //repsonse: undefined

在我与猫鼬的
find
通话中,我不断收到
undefined
返回。我的成绩 在我的导出中,文件不会被记录,但如果我在我的
项目之外返回一个简单字符串,它就会工作。find
调用

我正在传递
req
res
,它们正确地记录在我的导出文件中,因此不认为它们与问题有关。你知道怎么回事吗

routes.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};
导出/projects.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};
models/projects.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};

这是由于
Project.find()方法的异步性质造成的。您正试图在异步函数中返回一个值,该函数将在一段时间后完成。因此,当在
console.log('response:'+proj.test(req,res))中执行
proj.test(req,res)
时,is会返回未定义的值

解决方案 需要传递回调函数,该函数在
find
操作完成后执行

routes.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};
导出/projects.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};

这是由于
Project.find()方法的异步性质造成的。您正试图在异步函数中返回一个值,该函数将在一段时间后完成。因此,当在
console.log('response:'+proj.test(req,res))中执行
proj.test(req,res)
时,is会返回未定义的值

解决方案 需要传递回调函数,该函数在
find
操作完成后执行

routes.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};
导出/projects.js

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

app.use(function(req, res, next){
    //repsonse: undefined
    console.log('response: ' + proj.test(req, res));
    next();
});
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;
        });
    }
};
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);
app.use(function(req, res, next){

    proj.test(req,res,function(result){
      console.log('response',result);
    });
    next();
});
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);
        });
    }
};

回答得很好,谢谢,我知道这与此有关,但我在导出方面也有类似的情况,似乎不需要回调,但那一定是因为页面被呈现了。再次感谢您的全面回答!回答得很好,谢谢,我知道这与此有关,但我在导出方面也有类似的情况,似乎不需要回调,但那一定是因为页面被呈现了。再次感谢您的全面回答!