3
I’m using Nodejs + Mongodb, and I’m trying to send an array of Book, via method POST, but somehow it’s giving problem, because when using Postman and clicking Send, it just returns me:
{
    "_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 by Postman follows this template:
[
    {
        "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 just below here) via POST using Nodejs, but when I try, it returns the JSON above
Follows 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 method book save.() returns the completed data to me:
[ { 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' } ]
						
Your question is a little vague. Could explain a little better the problem and what you want to do?
– Luiz Felipe
What problem? Showing error in console?
– Costamilam
I got the description, tell me if it’s more readable, anything, I try to explain otherwise
– Wallace Baldenebre
It shows no error in the console, but does not send the data array correctly
– Wallace Baldenebre
Make a
console.log(req.body)before saving the book and ask the question– Costamilam
It shows the completed data @Guilhermecostamilam
– Wallace Baldenebre
That’s what I thought, it receives an array but tries to save as if it were an object, what happens is that the result when creating a Book it returns an object
undefined– Costamilam
Thank you guys, thank you all here, I got with the solution below!
– Wallace Baldenebre