I have been having trouble with this for a while.
I created a mongoose model, and have had plenty of successes using JSON and req.body to save data to the mongodb database through postman.
However, I am attempting to use arrays for the first time and I am running into trouble.
(I am using express and node.js btw)
My server has the following setup to receive the post route:
router.post('/data', function(req, res){
var data = new Data({
name: req.body.name,
price: req.body.price,
array: [{
name: req.body.array[0].name,
username: req.body.array[0].username,
bio: req.body.array[0].bio,
languages: [{language: req.body.array[0].languages[0].language}]
}]...
I have tried this with and without the [0]
before each array name. All of the values that are not arrays save fine to the database, but the arrays all have only one attribute, and that is the _id
and a bunch of jibberish after that.
My JSON is correct from what I have read..
{
"name": "Bill",
"price": 290,
"array": [{
"name": "Danny",
"username": "dnnyboy",
"bio": "Helo hello",
"languages": [{"language": "English"}]
}],...(the JSON list continues)
How do I properly save JSON array data into a mongoose model using req.body
? What is the syntax I am missing?