Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda