javascript - 修改后的对象属性数组然后传递给前端

标签 javascript node.js express

我正在使用 Node (express)并希望在我的响应(在这里我将其命名为结果)传递到前端之前添加一个新属性。

async.parallel(stack, function (err,result) {
  result.users[0].price = 100
  // console.log(result.users[0]); // I couldn't find price property here I wonder why.
  res.json(result);
});

为什么会这样呢?

我尝试更改其他属性,例如密码:

console.log(delete result.users[0].password);
console.log(result.users[0]) // password is still present here?

我在 fiddle 中尝试了一个小例子,它有效。 https://jsfiddle.net/16333z15/

最佳答案

router.get('/json', function (req, res) {
    var result = {
        users: [{
            id: 1,
            name: "abc"
        }]
    };
    result.users[0].price = 100;
    res.json(result);
});

我已经在 Express 中尝试过了,并且成功了。您应该检查结果类型,我认为它不是元组

关于javascript - 修改后的对象属性数组然后传递给前端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40070881/

相关文章:

javascript - 在 SVG 路径上实现 Div 对象

node.js - Meteor:WebSocket 握手期间出错:意外的响应代码:400

node.js - 用户在评论系统中的显示名称

javascript - 使用 Passport.js 和 Sequelize 引用多个模型

node.js - 使用 React Native 和 Nodejs Express Multer 上传图片

javascript - 解析 JavaScript 中的输入

javascript - VueRouter 在业力单元测试中未定义

javascript - typescript 访问带有句点(.)运算符的特定对象的对象的变量

node.js - 未找到命令 : ts-node-dev

node.js - 既然 connect 不再使用 parseCookie 方法,那么我们如何使用 express 获取 session 数据呢?