Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
How do i save a subdocument array to mondoDB

My model

const FormDataSchema = new mongoose.Schema({...})

// the Shipping schema
const ShippingSchema = new mongoose.Schema({
    itemstrackno : {
        type: String,
        minlength: [3, "Minimum characters for this field is 3"],
        maxlength: [100, "Maximum characters for this field is 100"],
        required: [true, "The Shipped item's tracking number is required"],
        trim: true,
        unique: true
    },
    formData: [FormDataSchema]
}, {timestamps: true})

const Shipping = mongoose.model("Shipping", ShippingSchema)
module.exports = Shipping

my controller

exports.postShipment = async (req, res, next) => {
const {itemstrackno, formdata} = req.body

try {
    const trackedItem = await Shipping.findOne({itemstrackno})

if(trackedItem){
    return next(new ErrorResponse("An Item with this track number exists", 400))
}

const shipment = await Shipping.create({itemstrackno, formdata})

res.status(200).json({
    success: true,
        data: shipment
    })
} catch (error) {
    next(error)
}
}

My postman request THE POSTMAN REQUEST

How do i structure the controller in a way that it saves the objects inside formData field

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!