javascript - 处理程序栏访问数组变量
问题描述
我正在 Express 应用程序中使用 Handlebars。从服务器端,我将日期数组作为字符串传递:
reqCtrl.dateController = async (req, res) => {
...
var dates = [
"2020-09-27",
"2020-09-28",
"2020-09-29",
"2020-09-30",
"2020-10-01",
"2020-10-02",
"2020-10-03",
"2020-10-04",
];
console.log(dates);
//Resulting in: ['2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30', '2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04']
res.render("requests/...",
dates
});
}
但是,在客户端,使用以下表达式我得到不同的值。
<script>
console.log({{ dates }})
//Resulting in: 1984 1983 1982 1981 2009 2008 2007 2006
</script>
我错过了什么吗?如何呈现格式正确的日期?
解决方案
如果您 render<p>{{dates}}</p>
而不是控制台记录它会发生什么?无论哪种方式,您都可能希望在使用把手时遍历数组以进行渲染。这是通过#each完成的。
<ul>
{{ #each dates}}
<li>{{this}}</li>
{{/each}}
</ul>
推荐阅读
- javascript - 如何禁用或启用 href 链接并更改其内容?
- amazon-web-services - Cloudwatch Insights 在多行日志中搜索
- java - Android Gradle如何更改java文件中变量或参数的类型
- charts - 使用 echarts 调整折线图系列符号的位置
- json - Spring Data JPA @OneToMany JSON响应与父级不返回第二个实例的父级
- node.js - 如何为 Google Cloud Function 设置重试配置(使用 Google Pub/Sub 安排的 Firebase 功能)-Node.js 8
- google-cloud-platform - 谷歌云:用于数据库内容维护的简单网络应用程序/用户界面?
- c# - 如何使用 SharePoint Webhook 检索通知信息
- shell - unix shell - 检查文件是否存在,名称中包含 YYYY-MM-DD
- sql - case when in case when 作为计算列