-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.js
88 lines (75 loc) · 2.67 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
77
78
79
80
81
82
83
84
85
86
87
88
var mongoose = require('mongoose');
// DB CONNECTION SETUP
var DATABASE_URL = process.env.MONGODB_DATABASE_URL;
mongoose.Promise = global.Promise;
mongoose.connect(DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("We are connected to the database!!");
});
//SCHEMA SETUP
// POST - title, content
var postSchema = mongoose.Schema({
title: String,
content: String
});
var Post = mongoose.model("Post", postSchema);
// USER - email, name
var userSchema = mongoose.Schema({
email: String,
name: String,
posts: [postSchema]
});
var User = mongoose.model("User", userSchema);
var newUser = new User({
email: "[email protected]",
name: "Andrew Michael"
});
newUser.posts.push({
title: "How to cook Mantu",
content: "Search the video in youtube. You will find lots of recipies and when you cook it share it with me."
});
newUser.save((err, savedUser) => {
if (err) {
console.log(err);
} else {
console.log(savedUser);
}
});
// Retrieving user and adding another post to it
User.findOne({
name: "Prince Charlie"
}, (err, retrievedUser) => {
if (err) {
console.log(err);
} else {
retrievedUser.posts.push({
title: "How to fry eggs",
content: "Come on everyone knows how to fry eggs, the secret is to keep the fire low."
});
retrievedUser.save((err, savedUser) => {
if (err) {
console.log(err);
} else {
console.log("*******************************************");
console.log(savedUser);
}
});
}
});
// var newPost = new Post({
// title: "How to cook Kabuli Paulao",
// content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Id consectetur purus ut faucibus pulvinar. Sollicitudin aliquam ultrices sagittis orci. Molestie a iaculis at erat pellentesque adipiscing commodo elit. Maecenas accumsan lacus vel facilisis volutpat est velit egestas dui. Pellentesque habitant morbi tristique senectus. Purus sit amet luctus venenatis lectus magna. Sed sed risus pretium quam vulputate dignissim suspendisse. Pellentesque habitant morbi tristique senectus et netus et malesuada. Elit pellentesque habitant morbi tristique senectus. Diam maecenas ultricies mi eget mauris pharetra et ultrices."
// });
// newPost.save((err,savedPost)=>{
// if(err){
// console.log(err);
// }
// else{
// console.log(savedPost);
// }
// });