Search code examples
node.jsmongodbexpresspostman

Getting an empty array while posting an an data


I am posting the data of the api through POSTMAN and mongoose and nodejs . I am building an API for podcast . And the API contains Category Name and the Episodes array which contains Episode Number , Episode Name , Episode Description and title image. After posting the data I am getting the Category Name but i am getting the Epiosdes Array empty.

Below is the API Create Code

const Create = async(req,res)=>{
    const CategoryName = req.body.CategoryName
    const EpisodeNo = req.body.EpisodeNo
    const EpisodeName = req.body.EpisodeName
    const EpisodeDescription = req.body.EpisodeDescription

    try{

        if(req.file){
            const file = dataUri(req).content;
            const result = await uploader.upload(file)
            const image = result.url;
            // console.log(image)
            const Podcast = new Podcasts({
                CategoryName:CategoryName,
                EpisodeNo:EpisodeNo,
                EpisodeName:EpisodeName,
                EpisodeDescription:EpisodeDescription,
                imageurl:image
            })
            console.log(imageurl)
            await Podcast.save()
            // console.log(Podcast)
            res.send(Podcast)
        }
    }catch(err){
        res.status(500).json(err)
        console.log(err)
    }
}

The Schema for API

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

Below is the API

{
"_id": "6472e8d3b36c95432b4758fc",
"CategoryName": "Music",
"Episodes": [],
"createdAt": "2023-05-28T05:38:27.135Z",
"updatedAt": "2023-05-28T05:38:27.135Z",
"__v": 0
}

This is the POSTMAN request

enter image description here

This is the code which worked

const Create = async(req,res)=>{
    const {CategoryName , Episodes} = req.body

    try{

        if(req.file){
            const file = dataUri(req).content;
            const result = await uploader.upload(file)
            const image = result.url;
            // console.log(image)
            const Podcast = new Podcasts({
                CategoryName:CategoryName,
                Episodes:[{
                    EpisodeNo:req.body.EpisodeNo,
                    EpisodeName:req.body.EpisodeName,
                    EpisodeDescription:req.body.EpisodeDescription,
                    imageurl:image
                }]
            })
            // console.log(imageurl)
            const Podcastresult = await Podcast.save()
            // console.log(Podcast)
            res.send(Podcastresult)
        }
    }catch(err){
        res.status(500).json(err)
        console.log(err)
    }
}

I think i am getting this problem due to req.body but i am not sure about that . Maybe i am posting the data of Episodes array in a wrong way . I want to solve the problem of the episodes array.


Solution

  • You need to send episodes as array in the request body like this:

    {
        "CategoryName": "Category 1",
        "Episodes": [
            {
                "EpisodeNo": 1,
                "EpisodeName": "name 1",
                "EpisodeDescription": "description 1"            
            },
            {
                "EpisodeNo": 2,
                "EpisodeName": "name 2",
                "EpisodeDescription": "description 2"            
            }
        ]
    }
    

    And arrange the code like this:

    const Create = async (req, res) => {
        const { CategoryName, Episodes } = req.body;
    
        try {
            if (req.file) {
                const file = dataUri(req).content;
                const result = await uploader.upload(file);
                const image = result.url;
                const podcast = new Podcast({
                    CategoryName,
                    Episodes,
                });
                const podcastResult = await podcast.save();
                res.send(podcastResult);
            }
        } catch (err) {
            res.status(500).json(err);
            console.log(err);
        }
    };
    

    I didn't include the image, because it is not clear whether you want to use the same imageUrl as the per episode.