Node.js猫鼬没有方法'save'
Node.js mongoose has no method 'save'
所以,这是错误:
/dev/sth-srv/app/services/notify.js:23
Notify.save(function() {
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'save'
这是模型(日期是字符串化的,因为我想确保我在这里没有做错任何事):
var mongoose = require('mongoose');
var notifySchema = new mongoose.Schema({
to : String,
type : String,
date : String,
what : String,
who : String,
status : String
});
mongoose.model('Notify', notifySchema);
module.exports = mongoose.model('Notify', notifySchema);
这是到达保存方法的对象
{
type: 'vote',
date: 'Sat Feb 21 2015 11:33:58 GMT+0100 (CET)',
what: 'asdgf12',
who: 'demouser',
status: 1,
to: 'demouser'
}
这是保存请求:
var notify = new Notify(notifyBody);
Notify.save(function() {
/.../
});
让我无法弄清楚的是,它在许多情况下都适用于其他模型,但不适用于这个特定模型。
最后重要信息:
这个保存函数不是直接从路由中调用的,而是从另一个带有 module.exports 的文件中调用的。 (需要型号)。
您需要在使用 new 关键字而不是模型创建的对象上调用保存。
var notify = new Notify(notifyBody);
//save needs to be called on notify, not Notify
notify.save(function() {
/.../
});
所以,这是错误:
/dev/sth-srv/app/services/notify.js:23
Notify.save(function() {
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'save'
这是模型(日期是字符串化的,因为我想确保我在这里没有做错任何事):
var mongoose = require('mongoose');
var notifySchema = new mongoose.Schema({
to : String,
type : String,
date : String,
what : String,
who : String,
status : String
});
mongoose.model('Notify', notifySchema);
module.exports = mongoose.model('Notify', notifySchema);
这是到达保存方法的对象
{
type: 'vote',
date: 'Sat Feb 21 2015 11:33:58 GMT+0100 (CET)',
what: 'asdgf12',
who: 'demouser',
status: 1,
to: 'demouser'
}
这是保存请求:
var notify = new Notify(notifyBody);
Notify.save(function() {
/.../
});
让我无法弄清楚的是,它在许多情况下都适用于其他模型,但不适用于这个特定模型。
最后重要信息: 这个保存函数不是直接从路由中调用的,而是从另一个带有 module.exports 的文件中调用的。 (需要型号)。
您需要在使用 new 关键字而不是模型创建的对象上调用保存。
var notify = new Notify(notifyBody);
//save needs to be called on notify, not Notify
notify.save(function() {
/.../
});