user2130951
user2130951

Reputation: 2741

Append to json with node

I am trying to append some information to a json response I get. But it doesn't seem like I can push information to the DISPLAY object I get.

The error I get is this. ReferenceError: DISPLAY is not defined Trying to push to fbResponse is not working either.

readCoin: function(myDB, url, connection) {
    console.log("URL: ", url);
    request(url, (error, response, body)=> {
  if (!error && response.statusCode === 200) {
    var fbResponse = JSON.parse(body);
    console.log("Got a response: ", fbResponse);
    //fbResponse[DISPLAY].push({ test: "testinfo"}); Fails here

Object information

testSetInt.js:9
_id:ObjectID {_bsontype: "ObjectID", id: Buffer(12)}
DISPLAY:Object {BTC: Object, ETH: Object, XRP: Object, …}
 ADA:Object {USD: Object, EUR: Object, SEK: Object}
 BCH:Object {USD: Object, EUR: Object, SEK: Object}
 BTC:Object {USD: Object, EUR: Object, SEK: Object}

Upvotes: 0

Views: 144

Answers (1)

Tnc Andrei
Tnc Andrei

Reputation: 1012

fbResponse[DISPLAY].push({ test: "testinfo"}); will fail because DISPLAY will be interpreted as a variable. Instead wrap DISPLAY in quotes: fbResponse["DISPLAY"].push({ test: "testinfo"});.

Or use the simpler syntax:

fbResponse.DISPLAY.push({ test: "testinfo"})

If you want to use DISPLAY as a variable, you have to define it

let DISPLAY = "DISPLAY";

fbResponse[DISPLAY].push({ test: "testinfo"});

Upvotes: 1

Related Questions