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

user8463989
  • 2,275
  • 4
  • 20
  • 48
  • I see in the schema you have `name: { type: String }` while you're passing an array of strings? – Michele Jul 03 '19 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 – Ravi Shankar Bharti Jul 03 '19 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 });` – user8463989 Jul 03 '19 at 13:35

2 Answers2

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 
});
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
  • 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 – user8463989 Jul 03 '19 at 13:42
  • 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 – Ravi Shankar Bharti Jul 03 '19 at 13:44
  • 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 Jul 03 '19 at 13:48
  • 1
    @user8463989 if you need each subdocument to have its own id either generate it yourself or make it its own collection – Isaac Vidrine Jul 03 '19 at 14:59
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.

CyberMessiah
  • 1,084
  • 11
  • 14