我正在使用MongoDB和Mongoose来创建数据对象,但是我在网页上显示这些对象时遇到了麻烦。我将以下查询添加到我的users.js文件中:
module.exports.getAllUsers = function(callback){
User.find({}, function(err, users) {
if(err){
res.send('error');
next();
}
res.json(users);
});
}
然后我使用把手在我的模板网页上显示用户列表:
{{#each users}}
<h4 class="name">{{user.name}}</h4>
{{/each}}
我的桌面上有用户,但我没有看到任何结果。我错过了什么?
谢谢!
编辑1:这是完整的users.handlebars文件:
<div class="main-container">
<section>
<div class="container">
<div class="row mb64 mb-xs-24">
<div class="col-sm-12 text-center">
<h3>users</h3>
<p class="lead">
</p>
</div>
</div>
</div>
</section><section>
<div class="container">
<div class="row">
<div class="col-md-8 col-sm-10 col-sm-offset-1 col-md-offset-2">
<div class="horizontal-tile">
{{#each users}}
<div class="tile-left">
<a href="#">
<div class="background-image-holder">
<img alt="image" class="background-image" src="img/project-single-1.jpg">
</div>
</a>
</div>
<div class="tile-right bg-secondary">
<div class="description">
<h4 class="mb8">{{this}}</h4>
<h6 class="uppercase">
Graphic Design
</h6>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
</div>
{{/each}}
</div>
</div>
</div>
</section></div>
这是users.js模型:
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
var UserSchema = new mongoose.Schema({
email: {
type: String,
index: true,
required: true,
trim: true,
minlength: 1,
unique: true,
},
password: {
type: String,
require: true,
minlength: 6
},
username: {
type: String,
require: true
},
tokens: [{
access: {
type: String,
required: true
},
token: {
type: String,
required: true
}
}]
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.createUser = function(newUser, callback){
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
newUser.save(callback);
});
});
}
module.exports.getUserByEmail = function(email, callback){
var query = {email: email};
User.findOne(query, callback);
}
module.exports.getUserById = function(id, callback){
User.findById(id, callback);
}
module.exports.getAllUsers = function(callback){
User.find({}, function(err, users) {
if(err){
res.send('error');
next();
}
res.json(users);
});
}
module.exports.comparePassword = function(candidatePassword, hash, callback){
bcrypt.compare(candidatePassword, hash, function(err, isMatch){
if(err) throw err;
callback(null, isMatch);
});
}
答案 0 :(得分:1)
请尝试使用{{this.name}}
。 this
表示正在迭代的元素。
答案 1 :(得分:0)
res.json()
,但你确定你有它吗?