0

I am setting up a NodeJS application and i need to get data from etcd. I am using the nodejs-etcd module.

The following is how I am attempting to return a value from a closure within a closure:

router.get('/', function(req, res, next) {

  var etcd = req.etcd;

  var result = etcd.read({key: '/reward/christopher'}, function (err, result, body) {

    // parse the json body so that it can be read
    object = JSON.parse(body);

    // return the value of the key
    return object.node.value;
  });

  console.log(result);

});

However all I ever get is the client connection data from the etcd object and not the value that I am trying to return:

Client {
  version: 'v2',
  baseurl: 'http://turtle-host-03.turtlesystems.local:4001/v2',
  agent: false }
GET / 500 807.246 ms - 1338
GET /stylesheets/style.css 304 6.856 ms - -

The value from the etcd server in the callback function is correct, it is just getting it into the outer function.

I am suspect this is a misunderstanding on my part as to how this closure works in the callback for the read method in etcd.

Update 1

Added code as requested instead of linking to a gist.

Russell Seymour
  • 1,333
  • 1
  • 16
  • 35
  • 1
    Can you put the code here as well. Gists files might be removed in the future and the code here stays for future visitors as well – drinchev Nov 25 '16 at 17:13
  • @drinchev I have updated the question with the code as requested. – Russell Seymour Nov 25 '16 at 17:56
  • Great. You can check the duplicated question's answer, because it's the answer of your own question. This question has been closed by the moderators, because the answer will be the same. – drinchev Nov 25 '16 at 17:59

0 Answers0