router.get("/list",******
*****
Articles.find({}).limit(20).select('content author_thumb').exec(function(err,articles){
return res.rend('list',{ articles: articles});
}
*****
....
//list.ejs
<% articles.forEach(function(article,index){ %>
<% include partial/article %>
<% }) %>
...
//partial/article.ejs
<h5><%= article.content %></h5>
<h4><%= article.get('content') %></h3>
<img src="<%= article.get('author_thumb')%>" alt=""
为什么不能用article.content的方式取值?
4 回复
好吧,找到为什么出错了,
//shema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var articleSchema = new Schema({
author: { type: String, default: ''},
author_thumb: { type: String, default: 'http://static.qiushibaike.com/images/thumb/missing.png'},
content: String,
att_img: {type:String, default: ''},
comment: { type: Number, default: 0},
up: { type: Number, default: 0},
created_at: { type: Date, default: Date.now()},
updated_at: { type: Date, default: Date.now()}
}, {collection : 'articles'})
******
******
exports = module.exports = articleSchema;//注释之后只能.get()的方式取值了。
//model.js
var mongoose = require('mongoose');
var schema = require('../schemas/article.js');
var model = mongoose.model;
mongoose.connect("mongodb://localhost/qiushibaike")
var Article = mongoose.model('Article',schema);
exports= module.exports = Article;
一不小心吧exports = module.exports = articleSchema 给注释了,注释之后只能.get的方式取值。取消注释之后就可以直接.title的方式取值了。至于为什么,请楼下高人回复!!