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

275
Visualizações
Post request from RESTful API giving UnhandledPromiseRejectionWarning: ValidationError: Product validation failed: error

I am working with this RESTful api and I want to send a post request to the server to create a document in the database. For that I am using model.create method. But it keeps me sending the error in the console UnhandledPromiseRejectionWarning: ValidationError: Product validation failed: seller: Please enter product seller, category: Please enter the product category, description: Please enter product description, name: Please enter product name I am testing this inside postman. Is there any bugs in my code? How can I fix this. Here is my app.js file

const express = require('express')
const mongoose = require('mongoose') 
const bodyParser = require("body-parser");

const app = express()
app.use(bodyParser.urlencoded({extended: true}));
mongoose.connect('mongodb://localhost:27017/playDB', {useNewUrlParser: true, useUnifiedTopology: true})

const playSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please enter product name'],
    },
    price : {
        type: Number,
        required: [true, 'Please enter product price'],
    },
    description: {
        type: String,
        required: [true, 'Please enter product description']
    },
    category: {
        type: String,
        required: [true, 'Please enter the product category'],
    },
    seller: {
        type: String,
        required: [true, 'Please enter product seller']
    },
    stock: {
        type: Number,
        required: [true, 'Please enter product stock'],
    }
})

const Product = mongoose.model('Product', playSchema)

//get all products

app.route("/products")
        .post(async function(req, res, next){
          const product = await Product.create(req.body);

          res.send({
              success: true,
              product
          })
            
        })


app.listen(3000, function(){
    console.log('server is running')
})

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

0

I didn't made express read express format. So that's why it didn't work

All I had to add was app.use(express.json()) Then it worked

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