简体   繁体   中英

Mongoose find not executing

Here's what I've got in a file called 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);

And then here's what I've got in a file called 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);
        });
    }
}

And then, I have,

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

The problem is that the .find() method doesn't give me anything. I get 'Finding zone' in my console, but absolutely nothing after that.

My folder structure is correct and I am referencing the correct files.

try this, I think that you must to return your find method in your controller. let me know if it work.

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

Here is a .find example for Tweet model in one of my old projects.

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

I think you must use .exec() in Model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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