-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcats.js
More file actions
55 lines (45 loc) · 1.38 KB
/
cats.js
File metadata and controls
55 lines (45 loc) · 1.38 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
47
48
49
50
51
52
53
54
55
var mongoose = require("mongoose");
mongoose.Promise = global.Promise; //overwrite promise error
mongoose.connect("mongodb://localhost/cat_app");
var catSchema = new mongoose.Schema({ //create schema (pattern) of info for database
name: String,
age: Number,
temperament: String
});
var Cat = mongoose.model("Cat", catSchema); //we took the schema (pattern) and compiled it into a model and saved it in to a variable called Cat.
//now we can use var Cat to do any amendments i.e. Cat.find or Cat.create (peer pattern)
//adding a new cat to DB
var george = new Cat({
name: "George",
age: 11,
temperament: "grouchy"
});
george.save(function(err, cat){ //create callback function so we get feedback on whether it saved or got an error with parameters of error and success
if(err) {
console.log("something went wrong..");
} else {
console.log("we just saved a cat to the DB");
console.log(cat);
}
});
Cat.create({
name: "Jerry",
age: 15,
temperament: "bland"
}, function(err, cat){
if(err) {
console.log(err);
} else {
console.log(cat);
}
});
//retrieve all cats from the DB and console.log each one
Cat.find({}, function(err, cats){
if(err) {
console.log("you fucked up, man.");
console.log(err);
} else {
console.log("ALL THE CATS...");
console.log(cats);
}
});