Hey,scott. 我提交评论之后无法redirect到原来的movieId.
我的comment数据库代码:
var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; var CommentSchema = new Schema({ movie: {type: ObjectId, ref: 'Movie'}, from: {type: ObjectId, ref: 'User'}, to: {type: ObjectId, ref: 'User'}, content: String, meta: { createAt: { type: Date, default: Date.now() }, updateAt: { type: Date, default: Date.now() } } }); CommentSchema.pre('save', function (next) { if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now() } else{ this.meta.updateAt = Date.now() } next() }); CommentSchema.statics = { fetch: function (cb) { return this .find({}) .sort('meta.updateAt') .exec(cb) }, findById: function (id, cb) { return this .findOne({_id: id}) .exec(cb) } }; module.exports = CommentSchema;
我的comment后台控制代码:
var Comment = require('../models/comment'); // comment exports.save = function (req, res) { var _comment = req.body.comment; var movieId = _comment.movie; var comment = new Comment(_comment); comment.save(function (err, comment) { console.log("comment:") console.log(comment) if (err){ console.log(err) } res.redirect('/movie/' + movieId) }) };
detail里面截取comment段代码:
// detail page exports.detail = function (req,res) { var id = req.params.id; Movie.findById(id, function (err, movie) { Comment .find({movie: id}) .populate('from', 'name') .exec(function (err, comments) { // console.log("name:") // console.log(comments) res.render('detail', { title: '淘通科技详情页', movie: movie, comments: comments }) }); }) };
最后是从comment后台文件设置console打印出来的字段:
comment: { reply: [], meta: { createAt: Fri May 06 2016 17:27:10 GMT+0800 (中国标准时间), updateAt: Fri May 06 2016 17:27:10 GMT+0800 (中国标准时间) }, _id: 572c636e1ac3d66803ba025a, content: '333', __v: 0 }
问题出在哪里现在一点思路都没有啊,,请各路大神帮忙看看。