-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb2.js
56 lines (44 loc) · 1.78 KB
/
mongodb2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// const express = require("express");
// const mongoose = require('mongoose');
// const app = express()
// app.use(express.json())
// mongoose.connect("mongodb+srv://iamadarsh1994:[email protected]/");
// const User = mongoose.model('Users', {name: String, email: String, password: String})
// app.post("/signup", async function(req, res){
// const username = req.body.name;
// const password = req.body.password;
// const name = req.body.name;
// const exisitingUser = await User.findOne({name: username});
// if(exisitingUser){ return res.status(400).send("Username already exixts") }
// const user = new User({name: name, password: password, email: username})
// user.save()
// res.json({
// "msg":"User created"
// })
// })
// app.listen(3000, function(){console.log("server is listening on 3000")})
const express = require("express");
const mongoose = require('mongoose');
const app = express();
app.use(express.json());
mongoose.connect("mongodb+srv://iamadarsh1994:[email protected]/");
const User = mongoose.model('Users', {name: String, email: String, password: String});
app.post("/signup", async function(req, res) {
const username = req.body.email; // corrected from req.body.name
const password = req.body.password;
const name = req.body.name;
const existingUser = await User.findOne({ email: username }); // corrected to check email
if(existingUser) {
return res.status(400).send("Username already exists");
}
const user = new User({name: name, password: password, email: username});
try {
await user.save(); // ensure the save operation completes
res.json({
"msg": "User created"
});
} catch (error) {
res.status(500).send("Error creating user");
}
});
app.listen(3000)