猫鼬找不到执行

时间:2017-01-22 10:53:56

标签: javascript node.js mongodb mongoose

以下是我在名为Zone.js的文件中所拥有的内容,

var mongoose = require('mongoose');
mongoose.set('debug', true);

var zoneSchema = new mongoose.Schema({
    name: {type: String, required: true, default: '', required: true},
    timestamp: {type: Date, default: Date.now, required: true},
    zipCodes: {type: [String], default: [], required: true}
});

module.exports = mongoose.model('Zone', zoneSchema);

然后是我在一个名为zoneController.js的文件中得到的内容,

var Zone = require('../models/Zone');

module.exports = {
    find: function(params, callback){
        console.log('Finding zone');
        Zone.find(params, function(err, zones){
            console.log('Got results');
            if (err){
                callback(err, null);
                return;
            }
            callback(null, zones);
        });
    }
}

然后,我有,

ZoneController = require('../controllers/zoneController');
ZoneController.find({}, function(err, results){
            console.log('Zone results received');
}

问题是.find()方法没有给我任何东西。我找到了'寻找区域'在我的控制台中,但在那之后绝对没有。

我的文件夹结构正确,我正在引用正确的文件。

2 个答案:

答案 0 :(得分:0)

试试这个,我认为你必须在你的控制器中返回你的find方法。 让我知道它是否有效。

module.exports = {
    return {
        find: function(params, callback) {
            console.log('Finding zone');
            Zone.find(params, function(err, zones) {
                console.log('Got results');
                if (err) {
                    callback(err, null);
                    return;
                }
                callback(null, zones);
            });
        }
    }
}

答案 1 :(得分:0)

以下是我的一个旧项目中Tweet模型的.find示例。

Tweet.find({}).sort({date: -1}).populate('_creator').exec((err, tweets) => {
        res.render('tweet-all', {title: `All Tweets`, tweets: tweets});
});

我认为你必须在Model中使用.exec()。