为什么我的 Mongoose findAll 方法返回 500 错误 ..?
Why is my Mongoose findAll method returning a 500 error ..?
这是我的模型/架构。 create 方法有效,但 "all" 方法不会.. 它 returns a 500..
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var DiSchema = new mongoose.Schema({
name: { type: String, lowercase: true , required: true },
photo: { type: String },
email: { type: String, unique: true, lowercase: true },
year: { type: Number},
timestamp: { type : Date, default: Date.now },
description: { type: String},
location: { type: Array },
social: {
website: {type: String},
facebook: {type: String },
twitter: {type: String },
instagram: {type: String }
}
});
DiSchema.methods.create = function(o, cb){
this.model.save(o, cb);
};
DiSchema.methods.all = function(o, cb){
Di.find(o, cb);
};
module.exports = mongoose.model('Di', DiSchema);
这是我的控制器:
'use strict';
var Di = require('../models/di');
exports.create = function(req, res){
Di.create(req.body , function(err, di){
console.log('req.body.di', req.body);
res.send({di:di});
});
};
exports.index = function(req, res){
Di.all({}, function(err, dis){
res.send({dis:dis});
});
};
路线:
var dis = require('../contollers/dis');
app.get('/dis', dis.index);
app.post('/dis', dis.create);
create 方法工作正常,但是当我使用 index / all 方法访问 get 端点时——我一直得到 500。
我可以将查询直接放在控制器中,而且它有效。
但是,当我尝试从模式方法中调用它时——它不会。
我是不是用错了方法?我的 JS 关闭了吗?
更新:
如果我将此代码放入控制器中,它会起作用:
exports.index = function(req, res){
Di.find({}, function(err, dis) {
if (!err){
res.send(dis);
};
});
};
仍然对 "why the method is not working from the model" 的原始问题感兴趣。
我也在模型中尝试过这种重构,但无济于事:
DiSchema.methods.list = function(o, cb){
this.model.find(o, cb);
};
&
DiSchema.methods.list = function(o, cb){
this.model('Di').find(o, cb);
};
好的,所以有几件事..
需要将模型更改为静态方法。模型的一种方法。
此外,还需要更改语法。
DiSchema.statics.all = function(o, cb){
this.model('Di').find(o, cb);
};
如果有人想以通俗易懂的方式更好地说明为什么需要进行这两项更改,我将很乐意更改正确答案的所有权。
这是我的模型/架构。 create 方法有效,但 "all" 方法不会.. 它 returns a 500..
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var DiSchema = new mongoose.Schema({
name: { type: String, lowercase: true , required: true },
photo: { type: String },
email: { type: String, unique: true, lowercase: true },
year: { type: Number},
timestamp: { type : Date, default: Date.now },
description: { type: String},
location: { type: Array },
social: {
website: {type: String},
facebook: {type: String },
twitter: {type: String },
instagram: {type: String }
}
});
DiSchema.methods.create = function(o, cb){
this.model.save(o, cb);
};
DiSchema.methods.all = function(o, cb){
Di.find(o, cb);
};
module.exports = mongoose.model('Di', DiSchema);
这是我的控制器:
'use strict';
var Di = require('../models/di');
exports.create = function(req, res){
Di.create(req.body , function(err, di){
console.log('req.body.di', req.body);
res.send({di:di});
});
};
exports.index = function(req, res){
Di.all({}, function(err, dis){
res.send({dis:dis});
});
};
路线:
var dis = require('../contollers/dis');
app.get('/dis', dis.index);
app.post('/dis', dis.create);
create 方法工作正常,但是当我使用 index / all 方法访问 get 端点时——我一直得到 500。
我可以将查询直接放在控制器中,而且它有效。
但是,当我尝试从模式方法中调用它时——它不会。
我是不是用错了方法?我的 JS 关闭了吗?
更新:
如果我将此代码放入控制器中,它会起作用:
exports.index = function(req, res){
Di.find({}, function(err, dis) {
if (!err){
res.send(dis);
};
});
};
仍然对 "why the method is not working from the model" 的原始问题感兴趣。
我也在模型中尝试过这种重构,但无济于事:
DiSchema.methods.list = function(o, cb){
this.model.find(o, cb);
};
&
DiSchema.methods.list = function(o, cb){
this.model('Di').find(o, cb);
};
好的,所以有几件事..
需要将模型更改为静态方法。模型的一种方法。
此外,还需要更改语法。
DiSchema.statics.all = function(o, cb){
this.model('Di').find(o, cb);
};
如果有人想以通俗易懂的方式更好地说明为什么需要进行这两项更改,我将很乐意更改正确答案的所有权。