skip to Main Content

Actually I am trying to push a new nested array inside an array through update request for an API. I am using all necessary middleware such as app.use(express.json()) and app.use(express.urlencoded({extended: true})); and also the multipart/form-data in the postman header but still I am getting the req.body undefined.

Below is the Update request code

const Update =async (req,res)=>{
    const _id = req.params.id
    const {Episodes} = req.body

    try{
        const UpdatedPodcast = await Podcasts.updateOne({
            _id
        },
        {
            $push:{
                Episodes:[{
                    Episodes
                }]
        }
        },
        {
            upsert:true,
            new:true
        }
        )
        console.log(Episodes)
        // console.log(data)
        res.send(UpdatedPodcast)
    }catch(err){
        res.status(500).json({
            message:"Error in updating the data"
        })
        console.log(err)
    }
}

Below is the schema for API data

const mongoose = require('mongoose')

const PodcastSchema = new mongoose.Schema({
        CategoryName:String,
        Episodes:[
            {
                EpisodeNo:Number,
                EpisodeName:String,
                EpisodeDescription:String,
                imageurl:String
        }
        ]
    },
    {
        timestamps:true
    })

module.exports = mongoose.model('PodcastSchema',PodcastSchema)

Below is the screenshot of POSTMAN request

enter image description here

I have tried to access the Episodes array through different method but still getting req.body undefined. Can someone help me to find solution to this problem.

2

Answers


  1. When looking on your screenshot, I notice that there a no field representing the Episodes field which is the array your looking for.

    So basically, it’s normal that your
    const {Episodes} = req.bodygive your null.

    Note that it’s not the req.body null, but the {Episodes} field which is absent.

    What I suggest:

    • Add some endpoint to update the Episodes field of a podcast. These endpoint can be:
    // 1. this allow you to add new add to your Episodes field.
    // Note: When posting a multi-format data like in your screenshot to this route, thing should go ok
    /podcast/[ID]/new-episode
    
    
    // 2. remove an episode from a podcast
    
    /podcast/[ID]/remove-episode/[IndexOfTheEpidode]
    
    
    //3. Remove the first episode of a podcast
    /podcast/[ID]/remove-first
    
    //3. Remove the last episode of a podcast
    /podcast/[ID]/remove-last
    
    // 4. Update an episode of a podcast 
    /podcast/[ID]/update-episode/[IndexOfTheEpidode]
    
    1. About your controller

    I think, the different controller for the above route are just dealing with array stuffs.

    1. About the Data you send to your API

    In my sample, [ID] is the podcast id and [IndexOfTheEpidode] is the index of the target episode in your podcast’s episodes array.

    Now you can use approximatively what is in your screen.

    Login or Signup to reply.
  2. make sure that you have included the express.json() middleware before your route handlers and before your route definition and also ensure that you are sending the request with the correct Content-Type header set to application/json. In Postman, make sure to select "raw" and "JSON (application/json)" options for the request body.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search