Search code examples
node.jsmongodbexpresspostmanmongoose-schema

Nested MongoDB document issue (Mongoose and Node Js)


I am facing some issues while inserting data into nested documents structure of mongoDb.

Following is the Mongoose Model:

const funnel = new mongoose.Schema({
funnelName:{
    type:String,
    unique:true
},
group: String,
category: String,
funnelStep: {
    stepType: String,
    stepName: String,
    stepPath: String,
    isTracking: Boolean,
    viewsStorage: []
} })

Below is the push I am sending to Db:

router.post('/createFunnel',async (req,res)=>{
if(!req.body.funnelName || !req.body.group || !req.body.category)
    {return res.status(422).json({error:"Please add all the fields."})}
try{
    const funnelSteps = []
    funnelSteps.push({
        stepType: req.body.stepType,
        stepName: req.body.stepName,
        stepPath: req.body.stepPath,
        isTracking: req.body.isTracking,
        viewsStorage: req.body.viewsStorage
    })
    const funnels = new Funnel({
        funnelName : req.body.funnelName,
        group : req.body.group,
        category : req.body.category,
        funnelStep : funnelSteps
    })
    await funnels.save(function(err){
        if(err){
            return res.status(422).send({error: err.message})
        }
        return res.json(funnels)
    })
} catch(err){
    return res.status(422).send({error: err.message})
} })

Below is the data structure I am sending through postman:

{
"funnelName":"Name-Funnel",
"group":"AVC",
"category":"XYZ",
"funnelStep":[
    {
        "stepType":"Advert",
        "stepName":"Angle",
        "stepPath":"google.com",
        "isTracking":1,
        "viewsStorage":[0,0]
    },
    {
        "stepType":"Optin",
        "stepName":"Ver 1",
        "stepPath":"fb.com",
        "isTracking":1,
        "viewsStorage":[1,0]
    },
    {
        "stepType":"Check",
        "stepName":"rev-cat",
        "stepPath":"google.com",
        "isTracking":0,
        "viewsStorage":[2,0]
    }
] }

Below is the output I am getting in response:

{
    "funnelStep": {
        "viewsStorage": []
    },
    "_id": "5ec0ff78a6dfab18f4210e96",
    "funnelName": "Testing The Latest Method4",
    "group": "AVC",
    "category": "XYZ",
    "__v": 0
}

How can I fix this issue as my data is not getting inserted properly?

And apart from this, in the viewsStorage array, how to store date and a number which will increment after a certain operations and will get saved in the array according to the dates?


Solution

  • I think there is an issue in the funnelSteps array creation part. You are trying to get data directly from req.body instead of req.body.funnelStep

    const funnelSteps = []
    req.body.funnelStep.forEach(fs => {
        funnelSteps.push({
            stepType: fs.stepType,
            stepName: fs.stepName,
            stepPath: fs.stepPath,
            isTracking: fs.isTracking,
            viewsStorage: fs.viewsStorage
        })
    })
    

    Schema

    const funnel = new mongoose.Schema({
    funnelName:{
        type:String,
        unique:true
    },
    group: String,
    category: String,
    funnelStep: [{
        stepType: String,
        stepName: String,
        stepPath: String,
        isTracking: Boolean,
        viewsStorage: []
    }] })