【发布时间】:2019-01-03 13:55:34
【问题描述】:
我将 api 天气数据传递给我的 ejs 模板。我有一个条件语句来检查是否存在任何 api 数据。无论如何,我总是收到以下错误:
无法读取未定义的属性“临时”
条件语句 if 在脚本标签内。
<script>
if ( JSON.parse('<%-locals.forecast.temps[0]%>') != undefined){
console.log('there is data');
var height = 500;
var width = 800;
var margins = {
left: 40,
top: 40,
right: 40,
bottom: 40
}
var barData = [
{strength:JSON.parse('<%-locals.forecast.temps[0]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[1]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[2]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[3]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[4]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[5]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[6]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[7]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[8]%>')},
{strength:JSON.parse('<%-locals.forecast.temps[9]%>')},
]
【问题讨论】:
-
你能告诉我们你将数据传递给模板的代码吗
-
locals.forecast未定义。 -
使用以下代码`res.render('index', {data:api_data, forecast:api_forecast});`将代码传递给我的routes.js文件中的ejs模板。我确实通过控制台日志确认 api 数据被正确提取。
-
那么
locals是什么?