0

Using the MERN stack I am able to add a document (a Property Schema in this case) via Mongoose. The issue is one of the Property Keys (Rooms in this case) is an Array of Objects. When I initially create the Property I don't send any data regarding the Rooms but a blank Object is created, albeit with a MongoDB _id?

I thought Mongoose prevented creating blank Objects / Arrays if no data was sent or am I confusing matters? Why is it happening? And is there a way to prevent this?

Just to be clear when I initially create the Property I'm sending no information and I don't even reference the rooms array in the data sent from axios.

Here is my Schema:

const propertySchema = new Schema({
    propertyId: String,
    propertyName: String,
    rooms: [
        rId: String,
        type: String,
        class: String,
        view: String,
        price: String
    ]
})

2 Answers 2

1

Arrays implicitly have a default value of [] (empty array).

But you can prevent it by giving a default: undefined option like this:

const propertySchema = new Schema({
  propertyId: String,
  propertyName: String,
  rooms: {
    type: [
      new Schema({
        rId: String,
        type: String,
        class: String,
        view: String,
        price: String,
      }),
    ],
    default: undefined,
  },
});

Docs (in the Arrays section)

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

2 Comments

Hi SuleymanSah, thanks for coming back to me. I had see this is the documentation and in other posts but it did not resolve my issue. However, after reading your suggestion it did prompt me to look at the Schema and Controller side by side. I realised that in the Property Controller there was no need to do define the Room Keys and assign the values from from req.body as I was handling this in the Rooms Controller.
@ssedwards I am glad you resolved the problem. I appreciate if you can mark as answer.
0

What I realised was I had two controllers, postPropertyController and patchPropertyController. As described in the question when I post the property for the first time I don't include anything in the req.body about the rooms. However, in the postPropertyController I was still doing this...

const propertySchema = new Schema({
    propertyId: String,
    propertyName: String,
    rooms: [
        rId: String,
        type: String,
        class: String,
        view: String,
        price: String
    ]
})

What I needed to do to clear the empty object in the rooms array was this...

const propertySchema = new Schema({
    propertyId: String,
    propertyName: String,
    rooms: []
})

Later in the application flow I used a patch method and the patchPropertyController to update the rooms array.

Shout out to @suleymanSah for suggesting something that made me take another look at the code side by side.

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.