2

I am trying to send an array of objects to mongodb using Postman but the subcategory array is always empty.

{
    "name": "Category 1",
    "subcategory": [
        {
            "name": "value 1, value2"
        }
    ]
}

My mongoose schema:

const categorySchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 5,
    maxlength: 255
  },
  subcategory: [
    {
      name: {
        type: String
      }
    }
  ]
});

My postman settings are "raw", JSON(application/json)

enter image description here

3
  • I see in the schema you have name: { type: String } while you're passing an array of strings? Commented Jul 3, 2019 at 13:18
  • can you show how are you saving it in your database? we have seen the schema and request body, please show the code which saves the request body to database collection Commented Jul 3, 2019 at 13:31
  • @RaviShankarBharti, ah yes. That is where the problem is. Just not sure how to fix it now that I look at it. let category = new Category({ name: req.body.name }); Commented Jul 3, 2019 at 13:35

2 Answers 2

3

The problem was you were not saving subcategory in your collection.

You can try :

let category = new Category({ 
    name: req.body.name, 
    subcategory : req.body. subcategory 
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this is almost right but how do I get each item in the array to have it's own ID? With this I get one _id with multiple values
what do you mean by each item have own idea? by default mongoose inserts _id at each level of object, kindly try the code and check your database
I have added a screenshot for you. Value 1 should have it's own _id and value 2 should also have it's own _id
@user8463989 if you need each subdocument to have its own id either generate it yourself or make it its own collection
0

I don't know if I get 100% right the question but I guess what you are trying to achieve is the following structure in MongoDB:

"_id": "524b1894066496c34b",
    "pickName": "Name",
    "chosenAddress": [
        {
            "_id": "24b1894066496c34d",
            "street": "Sample Street",
            "number": "321"
        },
        {
            "_id": "24b1894066496c34c",
            "street": "Second One",
            "number": "123"
        }

If that is the case, there is one workaround to obtain that. Before sending it to the Backend (POST request) make sure that you are converting the Object to Array of Objects. I suspect you are experiencing the same issue, trying to send Object instead of Array of Objects. Place this into your Submit Handle:

    var result = Object.keys(this.state.formControls).map(key => ({ key, value: this.state.formControls[key] }));   

The above reference: How to transpose a javascript object into a key/value array

Replace the key and value, accordingly.

Hope that will orient you, or somebody else may find it useful.

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.