Reputation: 812
I am writing the code using node.js and mongoose, I am stucked in the issue of posting the materials, where material is my entity.
Following is the schema:
new Schema({
title: {
type: String,
trim: true,
set: util.ucfirst,
required: true
},
description: {
type: String,
required: true,
trim: true
},
downloads:{
type: Array,
default: [],
required: true
},
course_id: {
type: String,
required: false
},
_status: {
required: false,
default: true,
type: Boolean,
select: false
},
created_by:{
type: String
},
created_at:{
type: Date,
default: Date.now
},
modified_by:{
type: String
},
modified_at:{
type: Date,
default: Date.now
}
},
{
collection: collection,
versionKey: true,
strict: true
})
Now using this data I am posting the sample data which accepts 2 "string" and 1 or more "file" data.
Following is the post api call:
exports.post('/', function(req,res,next){
var _error = req.mydata.get('error');
if( !_error ){
var _object = req.mydata.get('data') || {},
_files = req.mydata.get('files');
_object.downloads = (_files && Array.isArray(_files['upload'])) ? _files['upload'] : (_files && typeof _files['upload'] == "object") ? [_files['upload']] : [];
model.insert(_module, _object, function(err, entry){
if( !err && entry ){
res.status(200).json(entry);
res.end();
}else{
next();
}
});
}else{
next(_error);
}
});
But what I am receiving is the following output, which is not expected.
Upvotes: 3
Views: 2155
Reputation: 5717
I've just run into the same problem. You can't set versionKey
to true
, Mongoose thinks it's a new name for the version key - which should be a string. Just omit the versionKey
parameter and you'll be fine.
I've opened an issue for this: https://github.com/Automattic/mongoose/issues/3747
Upvotes: 5