[Fixed] My schema vehicles key in mongoose schema should not be empty

Issue

Hi everyone (newbie in express) I am creating a schema object whereby the key values are name: string, vehicles: array, image: string, content: string as below

const Logistics = mongoose.model('Logistics', new mongoose.Schema({
name: {
    required: true,
    type: String,
    minlength: 3,
    maxlength: 300
},
image: {
    type: String,
    required: true
},
vehicles: {
    type: Array,
    required: true,
},
content: {
    type: String,
    required: true
}}))

When I send a post request via postman, i get this

{
"vehicles": [],
"_id": "604a011dxxxxxxxxxxxxxxxx",
"name": "Air Freight",
"image": "path",
"content": "940 AirCargo is a Freight Forwarder capable of handling your time-sensitive cargo. We forward Air shipments to every Corner of the globe on a daily basis, ranging from fresh produce to Europe and Americas to Project Equipments to Mining and Power Companies in the Great lakes region. In addition we avail Cargo capacity daily to world destinations through signing block space Agreements with scheduled cargo/passenger carriers to ensure that our solutions to clients are seamless.",
"__v": 0

}

My problem here is vehicles shouldn’t be empty…I need help

Solution

I can’t comment you but I think the problem is not in the code provided. May be missing a {$push: {vehicles: req.body.vehicles}} when you save the data and retrieve it.

Leave a Reply

(*) Required, Your email will not be published