1

I'm using node (express) and want to add a new property before my response (in here I named it as result) get passed to the front-end.

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);
});

Why is this so?

I tried to alter the other property like password:

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

I tried a mini example in fiddle it worked. https://jsfiddle.net/16333z15/

6
  • what does the returned result look like? have you logged that? Commented Oct 16, 2016 at 13:38
  • @BennettAdams yes, it doesn't have the price property. Commented Oct 16, 2016 at 13:47
  • And what's async.parallel doing here, what exactly is asynchronous, and where are you trying to use it ? Commented Oct 16, 2016 at 14:01
  • @adeneo my full code. pastebin.com/5xj5Z8hh Commented Oct 16, 2016 at 14:08
  • Too complicated for me, looks like you're using MongoDB and doing some async calls to the DB, but I really don't understand how passing the enitre stack object to async.parallel(stack... would work ? Commented Oct 16, 2016 at 14:13

1 Answer 1

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

I have tried this in express and it's worked. You should check the result type, I think it's not tuple.

Sign up to request clarification or add additional context in comments.

1 Comment

I found the problem is using async, damn it this npm behave strangely, it doesn't give me the power to alter the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.