2

From nodeJS (with Express) I try to send JSON_array in response to client JS:

asksJsonArray = JSON.parse(fs.readFileSync("tasks.json", 'utf-8'));

app.get('/getArr', function (req, res) {
    readJsonContent();
    res.json(JSON.stringify(TasksJsonArray)); //sending JSON array to client_JS in response
});

On client-side I want to get it, but nothing receive:

$.get('/getArr').success(function(res) {
  var currencyData = JSON.parse(res);
  if (!currencyData.rates) {
    // possibly handle error condition with unrecognized JSON response
    alert("currency data not found!");
  } else {
    taskArr = currencyData;
  }
})

So I always receive msg 'currency data not found!' ...

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • In the clientside code try just console logging `res` and see what you get, most likely it's already an object and shouldn't be parsed again. Also, `res.json` parses things into valid JSON you don't have to stringify it. – adeneo Jun 25 '15 at 15:34
  • nothing shown in JS console( – Spacemaster Jun 25 '15 at 15:41
  • So `$.get` is getting nothing, but it's still entering the success callback? – adeneo Jun 25 '15 at 15:42
  • I receive response in success(function(res){ } So thats mean I receive response from nodeJS_side – Spacemaster Jun 25 '15 at 15:51

2 Answers2

2

res.json already converts the data to JSON, so you don't have to do it manually:

res.json(TasksJsonArray);

I believe this will also set the appropriate headers, so on the client, you don't have to explicitly parse the JSON, jQuery will do it for you:

$.get('/getArr').done(function(currencyData){
  if (!currencyData.rates) {
    // possibly handle error condition with unrecognized JSON response
    alert("currency data not found!");
  } else {
    taskArr = currencyData;
  }
});

Please note that assigning the response to a free variable is not really useful since you won't know when it's "safe" to access the variable. You might want to have a look at How do I return the response from an asynchronous call? .

This may still not work since currencyData might be a value that does not have a rates property. To learn how to correctly access the data, have a look at Access / process (nested) objects, arrays or JSON.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Yes, Express library also set headers. But if I use res(TasksJsonArray), so I receive in JS console 'Uncaught SyntaxError: Unexpected token o' that show on first line in my index.html ( – Spacemaster Jun 25 '15 at 15:48
  • Why are you using `res(TasksJsonArray)`? That should not even work. – Felix Kling Jun 25 '15 at 15:49
  • cause I have working example with such sending response: res.json({value: value}); Its work perfect – Spacemaster Jun 25 '15 at 15:53
  • I'm confused. In your first comment you say *"If I use `res(...)` ..."* and now your saying you use `res.json(...)`. Can you please decide on what you are using? – Felix Kling Jun 25 '15 at 15:54
  • 1
    Also did you remove the `JSON.parse` call from the client as shown in my answer? If you don't do that it won't work. I have the feeling you didn't really read my answer... – Felix Kling Jun 25 '15 at 15:56
  • sorry 4 mistake. I use 'res.json()' everywhere, because Express library send JSON response this way .. – Spacemaster Jun 25 '15 at 15:58
  • Yes, I did as you wrote but receive 'currency data not found!' message – Spacemaster Jun 25 '15 at 16:05
  • What is the output of `console.log(currencyData)`? You are most likely not accessing the data correctly. (you named your variable `TasksJsonArray`, so if this is really an array, then `currencyData.rates` cannot work, since arrays don't have such a property). Have a look at http://stackoverflow.com/q/11922383/218196 to learn how. – Felix Kling Jun 25 '15 at 16:05
  • In console Array[43] with my 43 objects – Spacemaster Jun 25 '15 at 16:09
  • As I said, arrays don't have a `rates` property (proof: `console.log([].rates)` logs `undefined`). One or more objects inside the array may have a `rates` property. You to decide now which of the 43 objects you want to access (or if you want to access all of them). Have a look at the link I posted, it should help you further. But least you have the data now. – Felix Kling Jun 25 '15 at 16:13
  • So maybe u know how to get values fron this JSON array? I see it in consile as Array[42] of objects. But when I try to get Arr[0].task - nothing shown( – Spacemaster Jun 25 '15 at 17:03
  • @Spacemaster: Since I don't know the data structure I can't help you. The linked question should contain all the information you need to figure it out on your own. – Felix Kling Jun 25 '15 at 17:07
  • > console.log(data.items[1]) Object id: 2 name: "bar" __proto__: Object This tells us that data.items[1] is an object, and after expanding it we see that it has three properties, id, name and __proto__. The latter is an internal property used for the prototype chain of the object. The prototype chain and inheritance is out of scope for this answer, though. – Spacemaster Jun 25 '15 at 17:15
0

Alter res.json(JSON.stringify(TasksJsonArray)); to res.send(JSON.stringify(TasksJsonArray));.

BrTkCa
  • 4,703
  • 3
  • 24
  • 45