0

I'm learning full stack development and on this project I'm working on I have a schema with the following:

var questionSchema = new mongoose.Schema(
  {
    language: String,
    precedence: [String],
    body: [{
      answer: String,
      correction: Number,
      mandatory: Number,
      eliminative: Number,
      points: Number
    }],
    type: String,
});

After initiliazing the connection to the server, I use Postman to try things out and whenever I do a POST, the "precedence": will always come before the "_id":. I had the same happening with the "body": but somehow i fixed it. It doesn't matter if the array is empty or not, it will still be on top. I'm going to start with my front-end development now, something I'm completely new to, and I have no idea if this is an error that can cause problems further ahead. How can i fix this?

  • 1
    Objects aren't ordered. It doesn't matter if `precedence` shows before `_id`. You will always access the properties the same way : `question._id` or `question.precedence`. – Jeremy Thille Mar 23 '21 at 14:39
  • 1
    Key ordering follows certain rules. See accepted answer here:https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – matttm Mar 23 '21 at 14:40
  • Also look at [Document Field Order](https://docs.mongodb.com/manual/core/document/#document-field-order) – Joe Mar 23 '21 at 17:08

0 Answers0