gpt4 book ai didi

javascript - Handlebars 中数组和对象的迭代

转载 作者:行者123 更新时间:2023-11-30 11:39:17 25 4
gpt4 key购买 nike

架构.js

var ItemSchema = mongoose.Schema({
username: {
type: String,
index: true
},
path: {
type: String
},
originalname: {
type: String
}
});

var Item = module.exports = mongoose.model('Item',ItemSchema, 'iteminfo');

路由.js

router.get('/', ensureAuthenticated, function(req, res){
Item.find({},function(err, docs){
res.render('welcome', {docs:docs});
});
});

索引.hbs

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
[list of data here!!!]
</body>
</html>

我在 route 的代码是否正确?如何在index.js中显示所有数据?请帮助。我是 node.jsmongoDB 的新手。谢谢:)

在 mongo shell 中,当我点击 db.users.find() 时,它有两个集合。我希望所需的输出在索引中是这样的

username = "username1"
path = "path1"
originalname = "originalname1"


username = "username2"
path = "path2"
originalname = "originalname2"

这可能吗?就像做一些 foreach 概念。

最佳答案

在你的 .hbs 文件中

You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over.

<ul class="people_list">
{{#each docs}}
<li>{{this}}</li>
{{/each}}
</ul>

对于对象在数组中的文档

[{}, {}, {}]

<ul class="people_list">
{{#each docs}} // iterating over array, and #each below loops the properties of elements {}
<li> {{#each this}} </li>
{{this}} // references the property values
{{/each}}
{{/each}}
</ul>

文档在这里

Iteration in Handlebars

关于javascript - Handlebars 中数组和对象的迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com