Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
req.body is undefined - only default mongodb id is getting pushed

I have been trying to use post request to insert data to the mongodb using mongoose, however I see that req.body is being shown as undefined. document is getting inserted in database, however only default object id is getting inserted.

This is how I send request to Postman

{ name:"wewe", price:43}

//Post.js

const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/tutorialkart");

const productSchema = mongoose.Schema({
    name: String,
    price: Number
});
module.exports = mongoose.model('Groduct', productSchema);

//app.js

var express = require('express')
var bodyParser = require('body-parser')
var Post = require('./Post')

var app = express()
app.use(bodyParser.json())



app.post('/api/posts', function (req, res, next) {
    var post = new Post({
        name: req.body.name,
        price: req.body.price
    })
    console.log("Log "+post.name );
    post.save(function (err, post) {
        if (err) { return next(err) }
        res.json(201, post)
    })
})

app.listen(3000, function () {
    console.log('Server listening on', 3000)
})

MongoDB

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There are two issues with your request in Postman (you removed the screenshot which showed these issues):

  • it's not formatted as proper JSON, but as a Javascript object
  • you're sending it as text/plain, not application/json
about 4 years ago · Juan Pablo Isaza Relatório

0

This is how I send request to Postman

{ name:"wewe", price:43}

One of two things is happening:

  • Either you aren't setting a Content-Type header in the request so the JSON parsing middleware you set up isn't being triggered or
  • You have, but since your JSON is invalid, it is failing to parse it.

You have to write valid JSON!

You can test your JSON with JSON Lint.

In particular, property names in JSON must be strings and cannot be identifiers. Strings in JSON must be delimited with " characters.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your screenshot is not visible/trouble opening the screenshot. You need to be more specific with your question.

  1. I think mongoose.connect("mongodb://localhost/tutorialkart"); here you need to specify the PORT localhost:27017

  2. Make sure you defined the names "name" and "price" in your HTML/EJS and setting their value as the data you are trying to insert into database.

  3. Use new before mongoose.model, mongoose.Schema

  4. app.use(bodyParser.json()) should be app.use(bodyParser.urlencoded({ extended: true }));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda