'Not able to add more than 1 post request in the mongo db
i am using mongo db to fetch and post the data but unable to post more than 1 request here is my code ON posting the different values second time i am getting the error I am unable to find the issue as i am using different values to post but still getting the same error
the very first portion of code. the error:
"driver": true,
"name": "MongoError",
"index": 0,
"code": 11000,
"keyPattern": {
"id": 1
},
"keyValue": {
"id": null
}
const express = require('express')
const app=express();
const cors=require('cors')
const mongoose=require('mongoose');
mongoose.connect('mongodb://localhost:27017/test1',{useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true}).then(()=>{console.log("connected")}).catch(err=>console.log(err));
const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());
const userroute=require('./routes/user')
app.use('/user',userroute);
app.listen(8000,()=>console.log("running on 8000"));
const mongoose = require('mongoose')
const userschema=new mongoose.Schema({
name:{
type:String,
required:true,
unique:false
},
img:{
type:String,
required:true,
unique:false
},
summary:{
type:String,
required:true,
unique:false
}
})
module.exports=mongoose.model("User",userschema)
const express=require('express')
const User = require('../model/User.model')
const router=express.Router()
router.get('/',(req,res)=>{
User.find().then(users=>res.json(users)).catch(err=>res.json(err))
})
module.exports= router;
router.post('/add',(req,res)=>{
const name=req.body.name;
const img=req.body.img;
const summary=req.body.summary;
const details = new User({name,img,summary});
details.save().then(()=>res.json(" added ")).catch(err=>res.json(err))
})
router.delete('/:id',(req,res)=>{
User.findByIdAndDelete(req.params.id).
then(()=>res.send("deleted")).catch((err)=>res.send(err));
})
Solution 1:[1]
Adding in answer to write code, in the comment section it becomes messy to understand the code.
router.post('/add',(req,res)=>{
const name=req.body.name;
const img=req.body.img;
const summary=req.body.summary;
const _id = mongoose.Types.ObjectId();
const details = new User({_id, name, img, summary});
details.save().then(()=>res.json(" added ")).catch(err=>res.json(err))
})
Ideally, mongoose should create _id automatically, but we can also create custom ids
Solution 2:[2]
I had exactly the same problem, what I did to solve it was download the latest version of mongodb, https://www.mongodb.com/try/download/community, update the path in the environment variables with the new version, download the latest version of mongo shell, finally restart the computer.
Solution 3:[3]
I was facing the same issue, just simply delete the collection from database and then make a new post request
Solution 4:[4]
Damusan's advice worked for me. This problem was insane because everyone who cloned my repo to help me had zero problems, so I knew it was on my end. The user collection accepted one user, then stopped and did exactly as described! The following fixed the problem:
OPEN THE MONGODB SHELL
mongo
MAKE SURE YOU HAVE THE RIGHT DB NAME=COMMAND TO LIST ALL DATABASES
show dbs
SWITCH TO THE DATABASE
use <name_of_your_db>
GET A LIST OF ALL YOUR COLLECTIONS TO CONFIRM USERS IS THERE
show collections
LIST ALL THE DATA IN THE USERS COLLECTION- IT SHOULD BE JUST ONE USER
db.users.find()
DROP THE COLLECTION -NO WORRIES, WHEN YOU MAKE A POST AGAIN IT WILL RE-CREATE THE USER COLLECTION
db.users.drop()
TRY TO LIST THE DATA AGAIN - IT SHOULD BE UNRESPONSIVE
db.users.find()
LIST YOUR COLLECTIONS AGAIN- IT SHOULD SHOW NO USERS COLLECTION
show collections
EXIT THE SHELL
exit
That's it, start the server and make a post for a user in Insomnia, and it should work, more than once!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | r7r |
Solution 2 | Damusan |
Solution 3 | Meraj Sheikh |
Solution 4 | Nimantha |