Hi people im doing the ecommerce website amazona with the name project, and i start to have a seriosly issue in the part of connect to MongoDB , when y tried to do the npm start in the project(amazona) the terminal write this issue, im going to anex my code
THIS IS THE CODE server.js
`import express from 'express';
import data from './data';
import dotenv from 'dotenv';
import config from './config';
import mongoose from 'mongoose';
import userRoute from './routes/userRoute';
Hi people im doing the ecommerce website amazona with the name project, and i start to have a seriosly issue in the part of connect to MongoDB , when y tried to do the npm start in the project(amazona) the terminal write this issue, im going to anex my code
THIS IS THE CODE server.js `import express from 'express'; import data from './data'; import dotenv from 'dotenv'; import config from './config'; import mongoose from 'mongoose'; import userRoute from './routes/userRoute';
dotenv.config();
const mongodbUrl = config.MONGODB_URL; mongoose.connect(mongodbUrl, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true }).catch(error => console.log(error.reason));
const app = express();
app.user("/api/users", userRoute); app.get("/api/products/:id", (req, res) => { const productId = req.params.id; const product = data.products.find(x=>x._id === productId); if(product) res.send(product); else
res.status(404).send({msg: "Product Not Found."}) });
app.get("/api/products", (req, res) => { res.send(data.products); });
app.listen(5000, () => {console.log("Server started at http://localhost:5000") });`
THIS IS THE CODE userRoute.js `import express from 'express'; import User from '../models/userModel';
const router = express.Router();
router.get("/createadmin", async (req, res) =>{ try{ const user = new User({ name: 'daniel', email: 'myemail@gmail.com', password: '1234', isAdmin: true }); const newUser = await user.save(); res.send(newUser); } catch (error) { res.send({msg: error.message}); } });
export default router;
**THIS IS THE CODE userModel.js**
import mongoose from 'mongoose';const userSchema = new mongoose.Schema ({ name:{ type: String, required: true}, email: {type: String, required: true, unique: true, dropDups: true}, password: {type: String, required: true}, isAdmin: { type: Boolean, required: true, default: false} });
const userModel = mongoose.model("User", userSchema);
export default userModel;`
THIS IS THE CODE config.js
export default { MONGODB_URL: process.env.MONGODB_URL || 'mongodb://localhost/project' }
THIS IS THE CODE package.json{ "name": "project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "nodemon --watch backend --exec babel-node backend/server.js" }, "author": "", "license": "ISC", "dependencies": { "dotenv": "^8.2.0", "express": "^4.17.1", "mongodb": "^3.5.9", "mongoose": "^5.9.22" } }
THIS IS THE CODE .envMONGODB_URL=mongodb://localhost/project