求解,无法获取populate来的值
发布于 9 年前 作者 ZhouQunying 3175 次浏览 最后一次编辑是 8 年前 来自 问答

直接上代码: // comment schema

var commentSchema = new Schema({ from: {type: Schema.Types.ObjectId, ref: ‘User’} })

// user schema

var userSchema = new Schema({ name: { unique: true, type: String } })

// controller

Movie.findById(id, function (err, movie) { Comment .find({movie: id}) .populate(‘from’, ‘name’) .exec(function (err, comment) { res.render(‘moviedetail’, { title: ‘详情页’, movie: movie, comment: comment }) }) })

jade中这样:

each item in comment h4 #{item.from}

那么输出将是:

{ _id: 56654e2cb6b20de90b92c9bf, name: ‘myName’}

而我jade中这样的时候:

each item in comment h4 #{item.from.name}

就报错: TypeError: app/views/pages/moviedetail.jade:21 19| img.media-object(src="", alt=“头像”, style=“width: 64px; height: 64px;”) 20| .media-body 21| h4 #{item.from.name} 22| p #{item.content} 23| hr 24| form(method=“POST”, action="/user/comment")

Cannot read property ‘name’ of null at eval (eval at <anonymous> (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:172:8), <anonymous>:213:52) at eval (eval at <anonymous> (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:172:8), <anonymous>:291:4) at eval (eval at <anonymous> (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:172:8), <anonymous>:658:22) at res (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:173:38) at Object.exports.render (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:269:10) at Object.exports.renderFile (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:305:18) at View.exports.renderFile [as engine] (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/jade/lib/jade.js:290:21) at View.render (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/express/lib/view.js:76:8) at Function.app.render (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/express/lib/application.js:504:10) at ServerResponse.res.render (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/express/lib/response.js:798:7) at Promise.<anonymous> (/Users/zhouqunying/learnProject/simpleMEAN/app/controllers/movie.js:27:21) at Promise.<anonymous> (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8) at emitOne (events.js:77:13) at Promise.emit (events.js:169:7) at Promise.emit (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38) at Promise.fulfill (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20) at Promise.resolve (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/lib/promise.js:114:23) at Promise.<anonymous> (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8) at emitOne (events.js:77:13) at Promise.emit (events.js:169:7) at Promise.emit (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38) at Promise.fulfill (/Users/zhouqunying/learnProject/simpleMEAN/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)

跪求大牛解释,真心感谢。

1 回复

请问这个问题解决了吗,我也遇到了同样的问题

回到顶部