-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.js
More file actions
46 lines (38 loc) · 1.12 KB
/
database.js
File metadata and controls
46 lines (38 loc) · 1.12 KB
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
// popular database driver to connect to mongodb
let mongoose = require('mongoose')
// security game airtight fam
mongoose.set('user', 'pillow')
mongoose.set('pass', 'fight')
let User = require('./backend/models/User')
mongoose.connect("mongodb+srv://cluster0-h3iy9.mongodb.net/test", {
useNewUrlParser: true,
dbName: "newsfeed",
poolSize: 5,
user: "pillow",
pass: "fight"
})
// create a new user to insert into the database
let user = new User({
username: "jmiller",
password: "phd",
firstname: "Jeffrey",
lastname: "Miller"
})
console.log(user.toJSON())
// save the new user to the database
user.save()
.then( (doc) => {
// print the output
console.log(doc)
})
// or catch the error
.catch( (err) => {
// if the error is code 11000
// Then MongoDB has thrown a duplicate key error, username is taken
console.log("User already exists");
// return res.status(500).send({ success: false, message: 'User already exist!' });
})
// mongoose.
// const Cat = mongoose.model('Cat', { name: String });
// const kitty = new Cat({ name: 'Zildjian' });
// kitty.save().then(() => console.log('meow'));