0

it might be a stupid question already but my purpose of this is to return an object called user where I can then use properly. http node.js documentation is full of console.log but nothing mentioned about returning objects. this is my code (which is from the standard documentation) where I also tried to assign the user or return it in the end method but it always return false.

var options= {
    host : 'localhost',
    port : 3000,
    path : '/users?userName=Rosella.OKon56', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};


callback = function(response) {
  var str = ''; 

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });
  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    //console.log(str);

  });

}
var req = http.request(options, callback);
req.end();
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
user3481262
  • 13
  • 1
  • 4
  • @quentin: No, not a dupe of that, he shows the (commented-out) code handling the response in an `end` event callback. Although that text right at the end...could be multiple issues here. – T.J. Crowder Feb 27 '15 at 10:22
  • @T.J.Crowder — He also says that he can find lots of documentation on how to use console.log but none on how to return. The only examples of console.log in the code are in asynchronous functions which can't be returned from. – Quentin Feb 27 '15 at 10:25
  • @Quentin: Yeah, there are multiple things in the above. Doing something other than using strings, and doing something with the result. – T.J. Crowder Feb 27 '15 at 10:26
  • @user3481262: You *don't* need to parse it? You're going to use a string? – T.J. Crowder Feb 27 '15 at 10:28
  • @user3481262: Hello? – T.J. Crowder Feb 27 '15 at 10:40

1 Answers1

1

Return something that you can then parse into an object structure. For instance, if you return JSON, you can use JSON.parse to parse it.

where I also tried to assign the user or return it in the end method but it always return false

If you want to do something with the parsed result, you would call a function with the result from within your end callback, like this:

callback = function(response) {
  var str = ''; 

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });
  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    doSomethingWithUserObject(JSON.parse(str));
  });
}

Side note: Your code is falling prey to The Horror of Implicit Globals; you need to declare your callback variable. You're also relying on Automatic Semicolon Insertion (you need a ; after your function expression, since it's not a declaration), which I don't recommend doing, but some people like.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @user3481262 : Duplicate: http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function – Quentin Feb 27 '15 at 10:27
  • @user3481262: In that case, Quentin was right, see here: http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function All that talk of a user object suggested you were having issues going from text to object. (The above also addresses this "return" thing, but the linked question has much, much more complete information.) – T.J. Crowder Feb 27 '15 at 10:27
  • Thank you both and I apologize if I duplicated – user3481262 Feb 27 '15 at 10:32