-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembed.js
76 lines (67 loc) · 1.61 KB
/
embed.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const mongoose = require('mongoose'); // npm install mongoose --save
// mongoose setup
mongoose.connect('mongodb://localhost:27017/blog_demo', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to DB!'))
.catch(error => console.log(error.message));
// POST - title, content
const postSchema = new mongoose.Schema({
title: String,
content: String
});
const Post = mongoose.model("Post", postSchema);
// USER - email, name
const userSchema = new mongoose.Schema({
email: String,
name: String,
posts: [postSchema]
});
const User = mongoose.model("User", userSchema);
// check if user model works
// const newUser = new User({
// email: 'hermione@brown.edu',
// name: 'Hermione'
// });
// newUser.posts.push({
// title: 'How to potion',
// content: 'jk, go to potions class to learn it'
// });
// newUser.save(function(err, user) {
// if (err) {
// console.log(err);
// } else {
// console.log(user);
// }
// });
// check if post model works
// const newPost = new Post({
// title: 'Reflections on apple',
// content: 'They are delicious'
// });
// newPost.save(function(err, post) {
// if (err) {
// console.log(err);
// } else {
// console.log(post);
// }
// });
User.findOne({name: 'Hermione'}, function(err, user) {
if (err) {
console.log(err);
} else {
// one to many
user.posts.push({
title: 'blog post',
content: 'this is a blog post'
});
// user.save(function(err, user) {
// if (err) {
// console.log(err);
// } else {
// console.log(user);
// }
// });
}
});