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

95
Visualizações
How to create multiple post request with same user id. I want to use the user id inside the car Schema using mongoose

const router = require("express").Router();
const Car = require("../../Model/CarSchema");
const { body, validationResult } = require("express-validator");
const auth = require("../../middleware/auth");

router.post(
  "/api/new-car",
  auth,
  [
    body("carName", "Car name is required").not().isEmpty(),
    body("carPrice", "Car price is required").not().isEmpty(),
    body("carDescription", "Car description is required").not().isEmpty(),
  ],
  async (req, res) => {
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    try {
      const car = new Car({
        _id: req.user,
        carName: req.body.carName,
        carPrice: req.body.carPrice,
        carDescription: req.body.carDescription,
      });

      const thecar = await car.save();
      res.status(200).json(thecar);
    } catch (err) {
      console.error(err.message);
      return res.status(500).json({ msg: "Server error" });
    }
  }
);

I want a user to make a multiple post request storing vehicles details in the database and I want each vehicles details to have the user id instead of having its own id. I was able to get the user id from my middleware. But whenever I try to make a post with same user id the second time, I get an error saying:===>

E11000 duplicate key error collection: car-project.cars index: id dup key: { _id: "6205065a5a02a28d9ba59546" }.

How do I make a multiple post requests with the same user id?

about 4 years ago · Juan Pablo Isaza
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