javascript – NodeJS – Problem sending a POST with object array

Question:

I'm using NodeJS + MongoDB, and I'm trying to send a Book array via method POST, but somehow it's giving a problem, because when I use Postman and click Send, it just returns:

{
    "_id": "5c48cf81a90935079bb12035",
    "createdAt": "2019-01-23T20:33:05.595Z",
    "updatedAt": "2019-01-23T20:33:05.595Z",
    "__v": 0
}

The JSON I'm trying to send to Postman follows this pattern:

[
    {
        "title": "A menina que roubava livros",
        "description": "Uma garota que adorava ir pra biblioteca roubar livro",
        "url": "http://www.amenina.com.br",
        "date": "2018-02-01",
        "owner": "Paulo Coelho"
    }, 
    {
        "title": "A menina que roubava livros 2",
        "description": "Uma garota que adorava ir pra biblioteca roubar livro",
        "url": "http://www.amenina.com.br",
        "date": "2018-02-01",
        "owner": "Paulo Coelho"
    }
]

In short: I need to send a Schema (which will be right below here) via POST using NodeJS, but when I try it returns the above JSON

Here are the related codes:

SCHEMA

[
    {
        title: String,
        description: String,
        url: String,
        data: String,
        owner: [String]
    }   
]

MODEL

const mongoose = require('mongoose')
const BookSchema = mongoose.Schema([
    {
        title: String,
        description: String,
        url: String,
        date: String,
        owner: String
    }], 
{
    timestamps: true
})
module.exports = mongoose.model('Book', BookSchema)

ROUTER

module.exports = (app) => {
    const books = require('../controllers/book.controller.js')
    app.post('/books', books.create)
    app.get('/books', books.findAll)
}

CONTROLLER

const Book = require('../models/book.model.js')
exports.create = (req, res) => {
    const book = new Book(
        {
            title: req.body.title || '',
            description: req.body.description || '',
            url: req.body.url || '',
            date: req.body.date,
            owner: req.body.owner
        }
    ) 

    book.save()
    .then(data => {
        res.send(data)
    }).catch(err => {
        res.status(500).send({
            message: err.message || "Erro em comunicação com o servidor. Tente mais tarde"
        })
    })
}

console.log(req.body) before the book.save() method returns me the filled data:

[ { title: 'A menina que roubava livros',
    description: 'Uma garota que adorava ir pra biblioteca roubar livro',
    url: 'http://www.amenina.com.br',
    date: '2018-02-01',
    owner: 'Paulo Coelho' },
  { title: 'A menina que roubava livros 2',
    description: 'Uma garota que adorava ir pra biblioteca roubar livro',
    url: 'http://www.amenina.com.br',
    date: '2018-02-01',
    owner: 'Paulo Coelho' } ]

Answer:

you're sending an array, but it's saving as if it were a single object. I recommend that you send an Array in this format.

{
books:[
    {
        "title": "A menina que roubava livros",
        "description": "Uma garota que adorava ir pra biblioteca roubar livro",
        "url": "http://www.amenina.com.br",
        "date": "2018-02-01",
        "owner": "Paulo Coelho"
    }, 
    {
        "title": "A menina que roubava livros 2",
        "description": "Uma garota que adorava ir pra biblioteca roubar livro",
        "url": "http://www.amenina.com.br",
        "date": "2018-02-01",
        "owner": "Paulo Coelho"
    }
]
}

that way you can access the JSON from req.body.books ; example:

    const books = req.body.books;

    for (let i = 0; i < books.books.length; i++) {
        const item = books[i];

        const book = new Book(
        {
            title: item.title || '',
            description: item.description || '',
            url: item.url || '',
            date: item.date,
            owner: item.owner
        }) 

        book.save()

    }
Scroll to Top