ejs的view里面怎么取得值啊?(新手勿喷)
发布于 10 年前 作者 igooda 6555 次浏览 最后一次编辑是 8 年前
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 回复

楼主用的是Mongoose吧,Mongoose查询结果返回的是mongodocument,不是普通的Object,所以需要使用get方法获取值

好吧,找到为什么出错了,

//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的方式取值了。至于为什么,请楼下高人回复!!

<% include partial/article %> 请问下你这里的partial文件夹跟当前模版目录在同一级吗?

partial目录是views目录下面的子目录啊。

回到顶部