1

I am making a mongodb model>>

const mongoose = require('mongoose');
const {Schema} = mongoose;

const locationSchema = new Schema({
    name: String,
    Address: String,
    ContactInfo: {
        phone: Number,
        email: String,
    },
    Website: String,
    Hours: {
        DaysOpen: String,
        OpeningTime:[Number],
        ClosingTime:[Number],
    },
    Services: String,
    Languages: String,
    Documentation: Boolean,
    OtherNotes: String,
})

mongoose.model('Locations', locationSchema);

When I try and run a get request to see what is in my database I am returned

  {
  "error": false,
  "location": {
    "Hours": {
      "OpeningTime": [
        1215,
        898
      ],
      "ClosingTime": [
        1400
      ],
      "DaysOpen": "Sunday"
    },
    "_id": "5ee8fd2e57aa5126d4c1c854",
    "name": "Evergreen Christian Center Food Pantry",
    "Address": "4400 NW Glencoe Rd, Hillsboro, OR 97124",
    "ContactInfo": {
      "phone": 5033196590,
      "email": "gonzocyn2@msn.com"
    },
    "Website": "https://www.ecc4.org/home",
    "Services": "All foods available including meat and frozen foods",
    "Languages": "English, Spanish",
    "Documentation": false,
    "OtherNotes": "Bring own bag or box. Sign up starts at 9:00am",
    "__v": 0
  }

The problem is that "Hours" is being displayed before the name, address, and contact info. This only occurs when I have the fields "OpeningTime" and "ClosingTime" as arrays. Any idea on how to fix this?

  • Does this answer your question? [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Joe Jun 16 '20 at 21:21
  • Ehh not really. – Stei0290 stein Jun 16 '20 at 22:39
  • Ok, a little more data, MongoDB [preserves field order](https://docs.mongodb.com/manual/core/document/index.html#document-field-order) with the exception of placing `_id` first. That field is obviously not first in the sample return, so the driver must be modifying the order after receiving the response from MongoDB. Since Mongoose runs on Node.JS, which uses Javascript, the field ordering will be as explained in the accepted answer on the question linked above. Except in those cases where the Mongoose internal clone function reverses the order. – Joe Jun 16 '20 at 23:23
  • OK got it now thanks!! – Stei0290 stein Jun 18 '20 at 18:29

0 Answers0