HTTP JSON in method and session.set with "JSON.parse(result.content)"

I made a method “myMeteorMethod” on server witch gives back data in JSON file

On the client side a call method "myMeteorMethod"and connect it to “myMethodResult” session and thanks to “JSON.parse(result.content)” i take first value “[0].node.title” from “nodes” table

How to take all values from “nodes” table.

if (Meteor.isClient) {

Meteor.call('myMeteorMethod', function(error, result){
  Session.set('myMethodResult', JSON.parse(result.content).nodes[0].node.title);
});

  Template.hello.helpers({
    counter: function(){
      return Session.get('myMethodResult'); //
    }
  });
}

if (Meteor.isServer) {
  Meteor.methods({
  'myMeteorMethod': function() {
    var result;
    result = HTTP.get('http://www.s1054489.home-whs.pl/meteor/json');
    return result;
  }
});

  Meteor.startup(function () {
    // code to run on server at startup
  });
}


<head>
  <title>yaz</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}
</body>

<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>

Replace…

with

Session.set('myMethodResult', JSON.parse(result.content).nodes.map(function(it){return it.node.title;}));

In case you get something like “map is not a function” run meteor add underscore to add the underscore package and then use this code:

Session.set('myMethodResult', _.map(JSON.parse(result.content).nodes, function(it){return it.node.title;}));

Suggestion: You (seem to) lack some basic JavaScript knowledge, like array iteration. You might benefit immensely from getting the basics down through some course like this one for example: http://www.codecademy.com/en/tracks/javascript

thank you for your help

1 Like