Mongoose TypeError: Object {} has no method 'cast'
Mongoose TypeError: Object {} has no method 'cast'
我试图将一个对象推入 Mongoose 数组,但出现以下错误:
TypeError: Object {} has no method 'cast'
at Array.MongooseArray._cast (/vagrant/kernl/node_modules/mongoose/lib/types/array.js:108:30)
at Object.map (native)
at Array.MongooseArray.push (/vagrant/kernl/node_modules/mongoose/lib/types/array.js:262:23)
at Promise.<anonymous> (/vagrant/kernl/routes/plugins.js:128:41)
at Promise.<anonymous> (/vagrant/kernl/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
at Promise.EventEmitter.emit (events.js:98:17)
at Promise.emit (/vagrant/kernl/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
at Promise.fulfill (/vagrant/kernl/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
at handleSave (/vagrant/kernl/node_modules/mongoose/lib/model.js:133:13)
at /vagrant/kernl/node_modules/mongoose/lib/utils.js:408:16
架构(插件)
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
PluginVersion = require('./PluginVersion');
var PluginSchema = new Schema({
name: { type: String, required: true },
description: { type: String },
created_date: { type: Date, default: Date.now },
active: { type: Boolean, default: true },
user: { type: Schema.Types.ObjectId, ref: 'User' },
versions: [PluginVersion]
});
module.exports = mongoose.model('Plugin', PluginSchema);
架构(插件版本)
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var PluginVersionSchema = new Schema({
version: { type: String, required: true },
downloads: { type: Number, default: 0 },
size: { type: Number, required: true },
updatedChecks: { type: Number, default: 0 },
fileName: { type: String, required: true }
});
module.exports = mongoose.model('PluginVersion', PluginVersionSchema);
错误代码
var file = req.files.file,
version = new PluginVersion();
version.version = req.body.version;
version.size = file.size;
version.fileName = file.path;
version.save(function(err) {
if(err) { res.send(err); }
plugin.versions.push(version); // <---- Problem.
plugin.save(function(err) {
if(err) { res.send(err); }
res.status(201);
res.json(version);
});
});
});
我对使用 Mongoose 还很陌生,所以我的知识可能存在差距。还有另一个与此类似的问题,但它提到了使用模式定义而不是模型定义的需要,我认为我已经正确地做到了这一点。
将一个模式嵌入到另一个模式时,您使用其模式而不是模型指定嵌入类型。所以 PluginSchema
应该像这样定义 versions
字段:
var PluginSchema = new Schema({
name: { type: String, required: true },
description: { type: String },
created_date: { type: Date, default: Date.now },
active: { type: Boolean, default: true },
user: { type: Schema.Types.ObjectId, ref: 'User' },
versions: [PluginVersion.schema]
});
我试图将一个对象推入 Mongoose 数组,但出现以下错误:
TypeError: Object {} has no method 'cast'
at Array.MongooseArray._cast (/vagrant/kernl/node_modules/mongoose/lib/types/array.js:108:30)
at Object.map (native)
at Array.MongooseArray.push (/vagrant/kernl/node_modules/mongoose/lib/types/array.js:262:23)
at Promise.<anonymous> (/vagrant/kernl/routes/plugins.js:128:41)
at Promise.<anonymous> (/vagrant/kernl/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
at Promise.EventEmitter.emit (events.js:98:17)
at Promise.emit (/vagrant/kernl/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
at Promise.fulfill (/vagrant/kernl/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
at handleSave (/vagrant/kernl/node_modules/mongoose/lib/model.js:133:13)
at /vagrant/kernl/node_modules/mongoose/lib/utils.js:408:16
架构(插件)
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
PluginVersion = require('./PluginVersion');
var PluginSchema = new Schema({
name: { type: String, required: true },
description: { type: String },
created_date: { type: Date, default: Date.now },
active: { type: Boolean, default: true },
user: { type: Schema.Types.ObjectId, ref: 'User' },
versions: [PluginVersion]
});
module.exports = mongoose.model('Plugin', PluginSchema);
架构(插件版本)
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var PluginVersionSchema = new Schema({
version: { type: String, required: true },
downloads: { type: Number, default: 0 },
size: { type: Number, required: true },
updatedChecks: { type: Number, default: 0 },
fileName: { type: String, required: true }
});
module.exports = mongoose.model('PluginVersion', PluginVersionSchema);
错误代码
var file = req.files.file,
version = new PluginVersion();
version.version = req.body.version;
version.size = file.size;
version.fileName = file.path;
version.save(function(err) {
if(err) { res.send(err); }
plugin.versions.push(version); // <---- Problem.
plugin.save(function(err) {
if(err) { res.send(err); }
res.status(201);
res.json(version);
});
});
});
我对使用 Mongoose 还很陌生,所以我的知识可能存在差距。还有另一个与此类似的问题,但它提到了使用模式定义而不是模型定义的需要,我认为我已经正确地做到了这一点。
将一个模式嵌入到另一个模式时,您使用其模式而不是模型指定嵌入类型。所以 PluginSchema
应该像这样定义 versions
字段:
var PluginSchema = new Schema({
name: { type: String, required: true },
description: { type: String },
created_date: { type: Date, default: Date.now },
active: { type: Boolean, default: true },
user: { type: Schema.Types.ObjectId, ref: 'User' },
versions: [PluginVersion.schema]
});