I have one embedded document as below:
var artist = new Schema({
name: {
type: String,
trim:true,
required:'Please fill artist name'
},
role : {
type: String,
trim: true,
default: ''
},
isPrimary : {
type: Boolean,
trim: false,
}
});
and other document is as below:
var AlbumSchema = new Schema({
language: {
type: String,
default: '',
trim: true
},
artists: [artist],
title: {
type: String,
default: '',
required: 'Please fill Album title',
trim: true
});
when I send request and princt req.body value than I get as below:
{
title: 'Demo Add',
artists:
'[{"name":"Ilesh","role":"Performer","isPrimary":true,"$$hashKey":"ob
ject:227"}]',
language:'EN'
}
but as soon as I cast it to object as
var album = new Album(req.body);
and print album than I get result as below:
{
title: 'Demo Add',
artists: [],
language: 'English'
}
I don't know why artists value becomes null during case.
EDIT :
exports.SaveAlbum = function(req,res){
var album = new Album(req.body);
var data = _.pick(req.body, 'type')
, uploadPath = path.normalize('/album_uploads')
, file = req.files.file;
var user = req.user;
album.cover_art_path = file.path;
if (user) {
var upsertData = album.toObject();
delete upsertData._id;
Album.update({_id: album.id}, upsertData, {upsert: true},function(err)
{
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}
else {
res.json(album);
}
});
} else {
res.status(400).send({
message: 'User is not signed in'
});
}
};