How to create a property on an existing object of an http request?

Asked

Viewed 157 times

3

I’m trying exhaustively to put a property on an object that comes from a request (JSON), but I’m not getting it, it’s like it just doesn’t, but when I do a console.log, there it is, but it’s not returning in the answer I send to Postman.

request(url, function (error, response, body) {
    let result = JSON.parse(body);
    result['teste'] = 'teste';
    res.status(200).send(result);
});

Without the property he returns me so:

[
    {
        "prop1": "prop1",
        "prop2": "prop2",
    },
    {
        "prop1": "prop1",
        "prop2": "prop2",
    },
]

And I wanted him to return something like this to me:

[
    {
        "prop1": "prop1",
        "prop2": "prop2",
    },
    {
        "prop1": "prop1",
        "prop2": "prop2",
    },
    "teste": "teste",
]

How do I stay that way up?

  • What are you trying to do after assigning the property? A new request?

  • @bfavaretto am picking up an external json from another url, I just want to add a property and return to the user as the request response.

  • What is "return to the user"? Your code handles the return of that JSON and adds a property to the obtained object. The use of the object needs to be done from within this function (callback) that you already have.

  • @bfavaretto this code is a part of an api, in which case we make a request for this route that fetches information in an external api that returns a json to me, I need to treat by adding an extra field at the end of json as in the example and return the modified json.

  • @Lollorcaust has already tried result.push({teste: 'teste'}); ?

2 answers

4

  • I tried that way, he just doesn’t put the element in the answer.

  • Here the problem may be that your request is not calling the request( ). Are you sure your code is actually running, and the request goes through it? Put LOG instructions in the middle, and check out.

  • Yes, I checked and it goes inside the function, I even printed only the result['test'] and it prints. Strange is also that if I return instead of result there, return result['test'] it returns the right test property in Postman.

  • I also tried to change the lib, instead of the request, to use Axios, but the same thing happens.

0

The return of the API is an array, to add a new element, using the method push.

Working example:

let result = JSON.parse('[{"prop1":"prop1","prop2":"prop2"},{"prop1":"prop1","prop2":"prop2"}]');
result.push({teste: 'teste'});
console.log(JSON.stringify(result));

Like you’re trying to:

let result = JSON.parse('[{"prop1":"prop1","prop2":"prop2"},{"prop1":"prop1","prop2":"prop2"}]');
result['teste'] = 'teste';
console.log(JSON.stringify(result));

this way will work if the return is an object:

let result = JSON.parse('{"prop1":"prop1","prop2":"prop2"}');
result['teste'] = 'teste';
// ou
result.testeB = 'teste b';
console.log(JSON.stringify(result));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.