[英]Mongoose find not executing
Here's what I've got in a file called Zone.js,这是我在一个名为 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,然后这是我在一个名为 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.问题是 .find() 方法没有给我任何东西。 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.试试这个,我认为你必须在你的控制器中返回你的 find 方法。 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 示例。
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.我认为您必须在模型中使用 .exec() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.