'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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
