0

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.

4
  • try defining steps in your schema using: steps: Object Commented Jul 25, 2014 at 21:18
  • No dice, still returns undefined on save. Commented Jul 25, 2014 at 21:23
  • You will get undefined in your middle block of code because you are attempting to set _id to an empty string, which is not a valid ObjectId type. If you remove the manual _id assignment, it should work. Commented Jul 25, 2014 at 21:33
  • Herp-a-DERP, it worked. Can you "answer" this so I can accept it? Thanks! Commented Jul 25, 2014 at 21:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.