I am having a hard time finding an error in this following code i wanted to connect to a database and add some data to it but iam getting an error whose info is specified below
**This is the code of Server.js
which is the server of the application **
import userdata from './userdata.js';
import data from "./data.js"
import mongoose from 'mongoose';
import userrouter from './routers/userrouter.js';
const app=express();
mongoose.connect('mongodb://localhost/purpleshades',{
useNewUrlParser:true,
useUnifiedTopology:true,
useCreateIndex:true
})
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use('/api/users',userrouter)
app.use((err,req,res,next)=>{
res.status(500).send({message:err.message})
})
const port=process.env.PORT || 5000
app.listen(5000,()=>{
console.log(`serve at http://localhost:${port}`);
})
This is the code of the userserver.js
which is the router file of the user router which handles the user many request
import userdata from "../userdata.js";
import User from "../models/usermodel.js";
import expressAsyncHandler from "express-async-handler";
const userrouter=express.Router()
userrouter.get('/seed',expressAsyncHandler( async(req,res)=>{
const createdusers=await User.insertMany(userdata.users)
res.send({createdusers})
}))
export default userrouter
The data file is asfollows which is called userdata.js
const userdata={
users:[
{
name:'Prashanth',
email:'[email protected]',
password:bcrypt.hashSync('1234',8),
isadmin:true
},
{
name:'ramesh',
email:'[email protected]',
password:bcrypt.hashSync('5678',8),
isadmin:true
},
{
name:'jhon',
email:'[email protected]',
password:bcrypt.hashSync('91011',8),
isadmin:true
},
{
name:'cristine',
email:'[email protected]',
password:bcrypt.hashSync('1213',8),
isadmin:true
}
]
}
export default userdata
Now the error is as follows
"message": "Operation `users.insertMany()` buffering timed out after 10000ms"
How do I get rid of this error.