-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-dishes.js
72 lines (64 loc) · 2.16 KB
/
server-dishes.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
var mongoose = require('mongoose'),
assert = require('assert');
var Dishes = require('./models/dishes');
// Connection URL
var url = 'mongodb://localhost:27017/anupama';
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
console.log("Connected correctly to server");
// create a new dish
Dishes.create({
"name": "Uthapizza",
"image": "images/uthapizza.png",
"category": "mains",
"label": "Hot",
"price": "4.99",
"description": "A unique . . .",
"comments": [{
"rating": 5,
"comment": "Imagine all the eatables, living in anupama!",
"author": "John Lemon"
},
{
"rating": 4,
"comment": "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
"author": "Paul McVites"
}
]
}, function(err, dish) {
if (err) throw err;
console.log('Dish created!');
console.log(dish);
var id = dish._id;
// get all the dishes
setTimeout(function() {
Dishes.findByIdAndUpdate(id, {
$set: {
description: 'Updated Test'
}
}, {
new: true
})
.exec(function(err, dish) {
if (err) throw err;
console.log('Updated Dish!');
console.log(dish);
dish.comments.push({
rating: 5,
comment: 'I\'m getting a sinking feeling!',
author: 'Leonardo di Carpaccio'
});
dish.save(function(err, dish) {
console.log('Updated Comments!');
console.log(dish);
db.collection('dishes').drop(function() {
db.close();
});
});
});
}, 3000);
});
});