diegohaz / bodymen

Body parser middleware for MongoDB, Express and Nodejs (MEN)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array of objects sending '[object Object]' to controller

peteashworth opened this issue · comments

I have the following field being send to node app (created off your generator btw)

links: [{'icon':'path to icon'}]

but once it gets to the controller, it fails due to its converted to '[object Object]'. Confirmed that the array of object is correct if I remove the

body({ links })

Suggestions or bug? Thanks

commented

Hi, @peteashworth

What is the value of links (the one you send as argument to body)?

I guess it's something like:

links: {
  type: String
}

If so, you need to change it to:

links: [{
  type: Object
}]

Maybe I should dive deeper to understand the package, but here is what I have currently.

Mongoose Model

links: [{
    icon: String,
    url: String
  }]

Route

const { ... links } = schema.tree
...
router.put('/', body({links})

I tried the type: Object within the route body() which gave me [ '[object Object]' ]

commented

Oh, I can reproduce it. I'll investigate what is happening. Good catch.

Cool; Thank you!

commented

Can you please do a fresh npm install and change body({ links }) to body({ links: [Object] })?

It should work now.

Thanks for the quick fix. Doing as suggested, I can console log the links and its now showing correctly. Saving it into mongo is a different story (back to [object Object]) but willing to bet its with the _.merge().

Thanks again!

all working. Thank you!

commented

Yay! 🎉

I was blocked by this problem too and found my solution here :)
I had a mongo validation error because every field of my body that were objects were rendered as [object Object].
You should add the use case with the :[Object] in your Readme ;)

Got this issue as well, but finally fixed it here. Would there be another solution to add required checks to the object parameters, maybe?