mongoose: Not able to persist array of objects in mongo using mongoose

Hi,

I’m trying to persist an array of objects in a document using mongoose. I have tried multiple times but it’s not persisting array in document. It places an empty array in document.

Following is my Schema:

var ProfileSchema = new Schema({

  name: String,
  PagesData: [{
    pageAccessToken: {type: String, get: decryptText, set: encryptText},
    category: String,
    name: String,
    id: String,
    perms: [String]
  }]

});

module.exports = mongoose.model('Profile', ProfileSchema);

I’m trying to save a document with an array of objects using following query:

var newProfile = new Profile();
newProfile.name = "someName";
newProfile.PagesData = [ { pageAccessToken: 'someToken',
    category: 'Bags/Luggage',
    name: 'someBrandName',
    id: '12345',
    perms: 
     [ 'ADMINISTER',
       'EDIT_PROFILE',
       'CREATE_CONTENT' ] } ];

newProfile.save(function(err, result, numAffected){
    if(err) {
        console.log(err);
        res.send(500, "Error");
    }
    console.log(result);
    res.send(200, "Success");
});

I tried debugging the mongo commands using

    require('mongoose').set('debug', true);

On Debug logs it shows, empty array during insert command execution.

Can anyone please tell me how can I store this array of object in my schema ?

Thanks,

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 24 (3 by maintainers)

Most upvoted comments

As a work around for my problem, I decided to update the array field separately, but somehow it is also not working.

        Profile.update({type: "facebook"}, {'PagesData':pagesArray}, {upsert:true}, function(err, result){
            if(err) {
                console.log(err);
                res.send(500, err);
            }
            console.log("inserting pagesData");
            console.log(result);
            res.send(200, "Success");
        });

Result: { ok: 0, n: 0, nModified: 0 }

Any idea why is this not working ? I think this is the minimum thing which one can get, right ?

Updates:

Above query works for simple data fields:

        Profile.update({type: "facebook"}, {'type':'google'}, {upsert:true}, function(err, result){
            if(err) {
                console.log(err);
                res.send(500, err);
            }
            console.log("inserting pagesData");
            console.log(result);
            res.send(200, "Success");
        });
Result: { ok: 1, nModified: 1, n: 1 }

There is something wrong in the array of objects field, that’s for sure. I’m surprised why your standalone script is working fine.

@vkarpov15 I have a similar problem too. example:


   // in req.body
   // regions_price: [{title: "wfe", price: 0, min: 0, free: 0}, {title: "wfwff", price: 2, min: 1, free: 30}]

    var self = this
      , id = req.params.id
      , body = req.body;

    this.model.findById(id, function(err, doc) {
      if (err || !doc) return done(err || new Err.NotFound('Document #' + id + ' not found'));
      // in doc.regions_price = [{title: "wfe", price: 0, min: 0, free: 0}];
      doc.set(body); 
      doc.save(function(err, document) {
        if (err) return done(err);

        // document not contain doc.regions_price[1] =  {title: "wfwff", price: 2, min: 1, free: 30};

        req.params.id = document.id;

        methods['get /:id'].call(self, req, res, done);
      });
    });

@vkarpov15 : Just for information, are you able to reproduce the issue ? Also, if this is a bug, then it will definitely take some time to be solved. Meanwhile, is there any work around which I can use for solving my case ?

Hmmm does it work if you remove the id field?