Skip to content

Commit 425f903

Browse files
author
zhuqianye
committed
god , I got it ,oh mongoose!
1 parent ccdc40d commit 425f903

File tree

2 files changed

+89
-85
lines changed

2 files changed

+89
-85
lines changed

practice/website/app.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ app.get('/movie/:id',function(req,res){
4646
if(id){
4747
console.log("进入了if这个判断里面");
4848
Movie.findById(id,function(err,movie){
49+
if(err){
50+
console.log("在这里出现了错误");
51+
}
52+
console.log(movie.title);
4953
res.render('detail',{
50-
movie:movie,
51-
title:'website 详情页'+ movie.title,
54+
title:"oh"+movie.title,
55+
movie:movie
5256
});
57+
//这里的意思其实给detail这个html文件传值
58+
console.log("这里已经走完了一次if");
5359
});
5460
}else{
55-
console.log("上面的id是存在的,但是这里可能出现了错误");
61+
console.log("没有找到id");
5662
}
5763
});
5864

@@ -74,7 +80,7 @@ app.get('/admin/update/:id',function(req,res){
7480
app.post('/admin/movie/new',function(req,res){
7581
// console.log("a"); 经过判断是下方这个语句出了问题
7682
var id=req.body.movie._id;
77-
// console.log("id is:"+id);
83+
console.log("在post这个过程中id是:"+id); //undefined
7884
var movieObj=req.body.movie;
7985
// console.log("movieObj is:"+movieObj);
8086
var _movie;

practice/website/schemas/movie.js

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,70 @@
1-
// var mongoose=require('mongoose');
1+
var mongoose=require('mongoose');
22

3-
// console.log("test");
3+
//关于这个Schema文件,主要为实例化为模型做准备,在没有实例化为model之前,没有办法操纵数据库
4+
var MovieSchema=new mongoose.Schema({
5+
doctor:String,//作者
6+
title:String,//标题
7+
country:String,//国家
8+
language:String,//语言
9+
year:String,//年份
10+
poster:String,//海报
11+
summary:String,//简介
12+
flash:String, //视频源
413

5-
// //关于这个Schema文件,主要为实例化为模型做准备,在没有实例化为model之前,没有办法操纵数据库
6-
// var MovieSchema=new mongoose.Schema({
7-
// doctor:String,//作者
8-
// title:String,//标题
9-
// country:String,//国家
10-
// language:String,//语言
11-
// year:Number,//年份
12-
// poster:String,//海报
13-
// summary:String,//简介
14-
// flash:String, //视频源
1514

15+
meta:{
16+
createAt:{
17+
type:Date,
18+
default:Date.now()
19+
},
20+
updateAt:{
21+
type:Date,
22+
default:Date.now()
23+
}
24+
}
25+
});
1626

27+
MovieSchema.pre('save',function(next){
28+
if(this.isNew){
29+
this.meta.createAt=this.meta.updateAt=Date.now();
30+
// 判断是否是新加的,若是新加的则让更新时间和创建时间一致
31+
// 若是再次保存,则只更新最新的更新时间
32+
}else{
33+
this.meta.updateAt=Date.now();
34+
}
35+
next();
36+
});
37+
38+
// 定义静态方法,静态方法在Model层就能够使用
39+
MovieSchema.statics={
40+
// 用fetch方法获取所有的数据
41+
fetch:function(callback){
42+
return this
43+
.find({})
44+
.sort('meta.updateAt')
45+
.exec(callback);
46+
// 根据更新的时间排序
47+
},
48+
findById:function(id,callback){
49+
return this
50+
.findOne({_id:id})
51+
.exec(callback);
52+
}
53+
};
54+
module.exports=MovieSchema;
55+
56+
57+
58+
// var mongoose = require('mongoose');
59+
// var MovieSchema = new mongoose.Schema({
60+
// doctor:String,
61+
// title:String,
62+
// language:String,
63+
// country:String,
64+
// summary:String,
65+
// flash:String,
66+
// poster:String,
67+
// year:String,
1768
// meta:{
1869
// createAt:{
1970
// type:Date,
@@ -22,82 +73,29 @@
2273
// updateAt:{
2374
// type:Date,
2475
// default:Date.now()
25-
// }
76+
// },
2677
// }
2778
// });
28-
29-
// MovieSchema.pre('save',function(next){
30-
// if(this.isNew){
31-
// this.meta.createAt=this.meta.updateAt=Date.now();
32-
// // 判断是否是新加的,若是新加的则让更新时间和创建时间一致
33-
// // 若是再次保存,则只更新最新的更新时间
34-
// }else{
35-
// this.meta.updateAt=Date.now();
79+
// MovieSchema.pre('save',function(next) {
80+
// if(this.isNew) {
81+
// this.meta.createAt = this.meta.updateAt = Date.now();
82+
// }
83+
// else{
84+
// this.meta.updateAt = Date.now();
3685
// }
3786
// next();
3887
// });
39-
40-
// // 定义静态方法,静态方法在Model层就能够使用
41-
// MovieSchema.statics={
42-
// // 用fetch方法获取所有的数据
43-
// fetch:function(callback){
88+
// MovieSchema.statics = {
89+
// fetch:function(cb) {
4490
// return this
45-
// .find({})
46-
// .sort('meta.updateAt')
47-
// .exec(callback);
48-
// // 根据更新的时间排序
91+
// .find({})
92+
// .sort('meta.updateAt')
93+
// .exec(cb);
4994
// },
50-
// findById:function(id,callback){
95+
// findById:function(id,cb) {
5196
// return this
52-
// .findOne({_id:id})
53-
// .exec(callback);
97+
// .findOne({_id:id})
98+
// .exec(cb);
5499
// }
55100
// };
56-
// module.exports=MovieSchema;
57-
58-
59-
60-
var mongoose = require('mongoose');
61-
var MovieSchema = new mongoose.Schema({
62-
doctor:String,
63-
title:String,
64-
language:String,
65-
country:String,
66-
summary:String,
67-
flash:String,
68-
poster:String,
69-
year:String,
70-
meta:{
71-
createAt:{
72-
type:Date,
73-
default:Date.now()
74-
},
75-
updateAt:{
76-
type:Date,
77-
default:Date.now()
78-
},
79-
}
80-
});
81-
MovieSchema.pre('save',function(next) {
82-
if(this.isNew) {
83-
this.meta.createAt = this.meta.updateAt = Date.now();
84-
}
85-
else{
86-
this.meta.updateAt = Date.now();
87-
}
88-
next();
89-
});
90-
MovieSchema.statics = {
91-
fetch:function(cb) {
92-
return this
93-
.find({})
94-
.sort('meta.updateAt')
95-
.exec(cb);
96-
},
97-
findById:function(id,cb) {
98-
return this
99-
.findOne({_id:id})
100-
.exec(cb);
101-
}
102-
};
103-
module.exports = MovieSchema;
101+
// module.exports = MovieSchema;

0 commit comments

Comments
 (0)