node.js - EJS模板中对象的显示属性

标签 node.js json express ejs

使用JSON.stringify后数据我可以看到输出,但我只想打印特定数据 EG _id :

//Node js 
return res.render('index', {user : req.session.user, robot : doc});

//ejs file
<%=  JSON.stringify(robot) %>

我得到的输出如下

[{
  "_id": "5c3afbbdfc0c371fe0d643be",
  "category": "robot",
  "name_of_Product": "Bipadle Robot",
  "product_img": "m1.jpg",
  "Front_img": "m1.jpg",
  "side_img": "m1.jpg",
  "price": "1080",
  "discount": "0%",
  "shiping_date": "12/01/2019"
}]

但我想打印_id仅。

我尝试使用<%= JSON.stringify(robot)[0]["_id"] %><%= JSON.stringify(robot)[0]._id %>甚至 <%= JSON.stringify(robot)._id %> 。我无法获得预期的输出。

最佳答案

您应该能够取消引用 EJS 模板中的 _id 字段:

//ejs file
<%= robot[0]['_id'] %>

由于_id的值已经是一个字符串,因此不需要将其字符串化。

关于node.js - EJS模板中对象的显示属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54168741/

相关文章:

node.js - 如何在 Express.js 布局中使用 partials?

javascript - 使用 twitter API 有时会出错

node.js - Webpack Config 为 Node 和 Browser 构建 sdk

javascript - 如何将链接文本更改为 HTML 中的图像

node.js - Express-handlebars 模板未在静态路由上加载 CSS 样式

javascript - 在 Javascript 循环内没有得到正确的结果

javascript - 将外部 json 文件转换为 html 表

javascript - Discord 机器人对话

javascript - Node.js 中的 Get/create 操作中未发现错误

javascript - 在 Node 上使用 Selenium 的 WebDriverJs 截屏的正确语法