2

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'
            });
        }

};
Ilesh Patel
  • 2,053
  • 16
  • 27

1 Answers1

1

There is a problem with the '' in the array as commented below. That was the problem in origin, when sending the req.body as told.

your req.body is:

{
      title: 'Demo Add',
      artists: '[{"name":"Ilesh","role":"Performer","isPrimary":true,"$$hashKey":"ob 
              ject:227"}]',
        language:'EN'
}

and it must be:

{
      title: 'Demo Add',
      artists: [{"name":"Ilesh","role":"Performer","isPrimary":true,"$$hashKey":"ob 
              ject:227"}],
        language:'EN'
}

This quotes are making the artists being a string, not an Array, and then it fails when saving.

Then, the solution is:

var album = new Album(req.body); 
album.artists = JSON.parse(req.body.artists);

Hope to help.

Thanks

  • Look then for the variable acting vas merge result (updated) and just add before the save comand the updated.markModified('artists') – Alejandro Teixeira Muñoz May 17 '15 at 10:30
  • from my question you can see my updted object has [] for artist so even after markModified it still blank. – Ilesh Patel May 17 '15 at 10:36
  • you are adding ' ' to define the artist when putting. Are you sure you need those ' ' ? I think it should be: `artists :[{"name":"Ilesh","role":"Performer","isPrimary":true,"$$hashKey":"ob ject:227"}];` – Alejandro Teixeira Muñoz May 17 '15 at 10:38
  • that is default value and that will apply if I have nothing to name – Ilesh Patel May 17 '15 at 10:39
  • Maybe you can post your save method on server so that we can analyze it? – Alejandro Teixeira Muñoz May 17 '15 at 10:51
  • I have issue with my req.body. it sends string instead of Array. after parsing JSON.parse(req.body.artists) and assign it to album.artists now it works as expected. Thanks for your help. – Ilesh Patel May 17 '15 at 13:19
  • I told you 2h ago... will change the answer and accept it, please! you are adding ' ' to define the artist when putting. Are you sure you need those ' ' ? I think it should be: artists :[{"name":"Ilesh","role":"Performer","isPrimary":true,"$$hashKey":"ob ject:227"}]; – Alejandro Teixeira Muñoz 2 hours ago – Alejandro Teixeira Muñoz May 17 '15 at 13:25