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();
5
  • @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. Commented Feb 27, 2015 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. Commented Feb 27, 2015 at 10:25
  • @Quentin: Yeah, there are multiple things in the above. Doing something other than using strings, and doing something with the result. Commented Feb 27, 2015 at 10:26
  • @user3481262: You don't need to parse it? You're going to use a string? Commented Feb 27, 2015 at 10:28
  • @user3481262: Hello? Commented Feb 27, 2015 at 10:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@user3481262 : Duplicate: stackoverflow.com/questions/6847697/…
@user3481262: In that case, Quentin was right, see here: stackoverflow.com/questions/6847697/… 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.)
Thank you both and I apologize if I duplicated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.