pretentiousgit
pretentiousgit

Reputation: 165

Mongoose - posting an object array to Mixed

Stumped with this one:

I have a pre-built object set called a "step" containing a bunch of sub-objects. It's already sanitized and set up properly, so I want to just shove it into Mongo whole.

I've got a Mongoose schema looks like this:

var SummarySchema = new Schema ({
    title : {type: String, trim: true},
    steps : {},
    created : Date,
    updated : Date,
    testKey : Number
})

Absolutely nothing will make it save - I've tried this, which gets me "undefined"

var summary = new Summary();


summary._id = '';
summary.user = req.body.user;
summary.testKey = req.params.testId;
summary.steps = req.body.steps;

summary.save(function(err, data, number) {
            if (err)
                res.send(err);
            console.log('I have added and saved a summary', data);
    });

console.log (summary); gets me

summary  { steps: 
   [ { tags_single: [Object],
       pass_fail: false,
       session_by_user: [Object],
       name: 'Apollonius of Perga' },
     { tags_single: [Object],
       pass_fail: false,
       session_by_user: [Object],
       name: 'Orion\'s sword' } ],
  testKey: 184702356266,
  _id: 53d2ca9e61b11bab40000004 }

and a variety of for-loops to push the steps into the DB. In every case, it simply doesn't save. I can't tell why it wouldn't at least save an empty structure, but it fails totally instead.

Can you not just push things into a mixed object? What's the correct way of doing this? Even loops to return the data in a "tidier" way fail.

Upvotes: 0

Views: 157

Answers (1)

scarlz
scarlz

Reputation: 2512

The problem is that you're attempting to assign an empty string as an _id for your document, which is not a valid ObjectId type.

Omit this assignment and the document will save correctly using the _id already created by the Summary constructor.

Upvotes: 1

Related Questions