【发布时间】:2017-07-31 04:18:43
【问题描述】:
所以,我不确定这里发生了什么以及为什么要在 JSON 键名中加上句点。
我正在尝试做的概述是通过 ejs 变量将 json 响应传递到页面模板中,并从中获取各个字段中的数据。
json 响应如下所示:
来自 prismic.io。 (打开对象括号在那里被切断,数据是主对象的子对象)。
当我通过 EJS 注入时
<%= product.data.product.imgone2.value.main.url %>
我收到如下错误:
Cannot read property 'imgone2' of undefined
哪个,棱镜为什么要这样做?
有没有办法用 EJS 解决这个问题?
如果没有,我如何使用 javascript 函数解析 JSON 响应以删除它?
如果你需要我的路线:
router.get('/product/:slug', function(req, res) {
//route params
var slug = req.params.slug;
var productResp; //scope up api response to pass to render()
console.log(slug);
//api call
Prismic.api("https://prismic.io/api").then(function(api) {
return api.getByUID('product' , slug);
}).then(function(response) {
res.render('product-template', {
product: response,
})
}, function(err) {
console.log("Something went wrong: ", err);
});
});
谢谢!
【问题讨论】:
-
你试过
product.data["product.imgone2"].value.main.url吗? -
括号符号 FTW!
标签: javascript json node.js express ejs