Home > Mobile >  How can i send array of object in postman?
How can i send array of object in postman?

Time:03-14

I have this schema and whenever i try to post request using postman, it returns me error as basic is of required type "String" but i have defined it as number. I am also having trouble posting array of objects for bonuses and deductable. It is posting empty array. What is the way to post array of objects in postman?

const LevelSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  description: {
    type: String
  },
  basic: {
    type: Number,
    required: true
  },
  bonuses: [
    {
      name: {
        type: String,
        required: true
      },
      amount: {
        type: Number,
        required: true
      }
    }
  ],
  deductables: [
    {
      name: {
        type: String,
        required: true
      },
      amount: {
        type: Number,
        required: true
      }
    }
  ],
  is_delete: {
    type: Number,
    default: 0
  }
});

CodePudding user response:

You can change type as Array for bonuses, deductables and create another schema for the object that needs to be stored in the array

bonuses :{
    type : Array,
    required : true

}

CodePudding user response:

You can create a new schema for bonuses and deductables. Because the share all properties, you can even use the same schema for both. For example:

const BonusesDeductableSchema = new Schema({ 
    name: { type: String, required: true},
    amount: { type: String, required: true},
});

And then, apply this schema in your LevelSchema:

const LevelSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String
    },
    basic: {
        type: Number,
        required: true
    },
    bonuses: [BonusesDeductableSchema],
    deductables: [BonusesDeductableSchema],
    is_delete: {
        type: Number,
        default: 0
    }
});

I think this should work.

Then, in your postman, something like this:

{
    "name": "string",
    "description": "string",
    "basic": 1,
    "bonuses": [
        {
            "name": "string",
            "amount": 1
        },
        {
            "name": "string",
            "amount": 1
        }
    ],
    "deductables": [
        {
            "name": "string",
            "amount": 1
        },
        {
            "name": "string",
            "amount": 1
        }
    ],
    "is_delete": 1
}

Hope I helped.

  • Related