无法获取特定 属性 的 js 对象
Can't get a specific property of js object
我正在使用 ejs、mongodb 和 express。在 ejs 文件中,我将编辑按钮的值设置为传递到 ejs 文件中的 js 对象,这样我就可以在编辑按钮向路由发出 post 请求后快速查询所需数据。
EJS 编辑按钮代码:
<% listOfRecords.forEach((singleRecord)=>{ %>
<div class="card card-body">
<form method="post">
<button formaction="/edit" class="btn btn-success btn-sm" name="editBtn" value="<%=singleRecord%>">Edit</button>
</form>
</div>
<% }); %>
但是,我可以通过以下代码在 express 中控制台记录 js 对象:
app.post('/edit', (req, res)=>{
console.log(req.body.editBtn);
});
以上代码的输出为:
{
_id: 60605148a1fba61fd8f2446f,
title: 'asdf',
author: 'sadf',
class_section: 'asfd',
dateIssued: 2021-03-01T00:00:00.000Z,
__v: 0
}
但是当我尝试这样做时:
console.log(req.body.editBtn.title);
它显示错误,undefined
我做错了什么?
我认为我们没有足够的信息。从我的角度来看,代码看起来不错。它应该有效。
您可以尝试通过 console.log(req.body.editBtn['title']);
而不是 console.log(req.body.editBtn.title);
来获取属性。
您也可以尝试解构标题:const { title } = req.body.editBtn
.
虽然这些理论上应该行不通!
也许您的代码中的其他地方是错误的?
编辑:
如果 req.body.editBtn
是一个字符串然后尝试 JSON.parse(req.body.editBtn);
然后得到你想要的属性。
真正的问题是 req.body.editBtn
是字符串格式。因此,为了使这项工作有效,EJS 文件中的更改将是:
<button formaction="/edit" class="btn btn-success btn-sm" name="editBtn" value="<%=JSON.stringify(singleRecord)%>">Edit</button>
这会将js对象正确转换为字符串,然后在express文件中,变化是:
let editBtn = req.body.editBtn;
let info = JSON.parse(editBtn);
现在我可以访问对象的任何 属性,因为它已正确转换为字符串和从字符串转换。
您应该从 JSON.parse 方法的输出中获取属性,而不是 req.body.editBtn 仍然是字符串。
app.post('/edit', (req, res)=>{
const data = JSON.parse(req.body.editBtn);
// console.log(req.body.editBtn);
console.log(data.title);
console.log(data.author);
// ....
});
我正在使用 ejs、mongodb 和 express。在 ejs 文件中,我将编辑按钮的值设置为传递到 ejs 文件中的 js 对象,这样我就可以在编辑按钮向路由发出 post 请求后快速查询所需数据。 EJS 编辑按钮代码:
<% listOfRecords.forEach((singleRecord)=>{ %>
<div class="card card-body">
<form method="post">
<button formaction="/edit" class="btn btn-success btn-sm" name="editBtn" value="<%=singleRecord%>">Edit</button>
</form>
</div>
<% }); %>
但是,我可以通过以下代码在 express 中控制台记录 js 对象:
app.post('/edit', (req, res)=>{
console.log(req.body.editBtn);
});
以上代码的输出为:
{
_id: 60605148a1fba61fd8f2446f,
title: 'asdf',
author: 'sadf',
class_section: 'asfd',
dateIssued: 2021-03-01T00:00:00.000Z,
__v: 0
}
但是当我尝试这样做时:
console.log(req.body.editBtn.title);
它显示错误,undefined
我做错了什么?
我认为我们没有足够的信息。从我的角度来看,代码看起来不错。它应该有效。
您可以尝试通过 console.log(req.body.editBtn['title']);
而不是 console.log(req.body.editBtn.title);
来获取属性。
您也可以尝试解构标题:const { title } = req.body.editBtn
.
虽然这些理论上应该行不通! 也许您的代码中的其他地方是错误的?
编辑:
如果 req.body.editBtn
是一个字符串然后尝试 JSON.parse(req.body.editBtn);
然后得到你想要的属性。
真正的问题是 req.body.editBtn
是字符串格式。因此,为了使这项工作有效,EJS 文件中的更改将是:
<button formaction="/edit" class="btn btn-success btn-sm" name="editBtn" value="<%=JSON.stringify(singleRecord)%>">Edit</button>
这会将js对象正确转换为字符串,然后在express文件中,变化是:
let editBtn = req.body.editBtn;
let info = JSON.parse(editBtn);
现在我可以访问对象的任何 属性,因为它已正确转换为字符串和从字符串转换。
您应该从 JSON.parse 方法的输出中获取属性,而不是 req.body.editBtn 仍然是字符串。
app.post('/edit', (req, res)=>{
const data = JSON.parse(req.body.editBtn);
// console.log(req.body.editBtn);
console.log(data.title);
console.log(data.author);
// ....
});