我觉得这真的很奇怪,我是json通过 http 发送的。
{
"foo":"bar",
"foo2":"bar2",
"name":{
"firstName":"Joff",
"middleName":"Ramirez",
"lastName":"Tiquez"
}
}
Run Code Online (Sandbox Code Playgroud)
在服务器上我正在执行这些命令:
var data = req.body; // the json from http
console.log('data', data); // the data now has the req.body's value
delete data.name; // <-- here's the delete
console.log('data', data); // the name object will obviously be deleted
console.log('req.body', req.body); // the name on the req.body was deleted too. Wtf?
Run Code Online (Sandbox Code Playgroud)
req.body.name因此,当我尝试在程序的其他部分使用 时,name现在已经消失了。那是delete应该如何工作吗?
var data = JSON.parse(JSON.stringify(req.body));
delete data.name; // <-- here's the delete
Run Code Online (Sandbox Code Playgroud)
现在当你做一个
console.log('req.body', req.body); // This won't be deleted.
Run Code Online (Sandbox Code Playgroud)
正如@Dellirium指出的,对象是通过引用传递的,reqBody是与数据相同的对象