IGOR LEVKOVSKY
IGOR LEVKOVSKY

Reputation: 167

Returning object in http response with NODE.JS

I want to send an sql query result object from http server back to the client. I get this error: "TypeError: First argument must be a string or Buffer"

If I turn the response content into string it works fine, but then it becomes useless for me.

What is the solution for the problem? Maybe there another way of making http response, or something else?

retrieve.retrieveAllStores(function(data){ 
  res.writeHead(200, {'Content-Type': 'text/plain'}); 
  res.write(data); 
  res.end(); 
});

Upvotes: 0

Views: 1890

Answers (1)

codejockie
codejockie

Reputation: 10844

You can try using JSON.stringify() to convert data to a string and then using JSON.parse() to convert back to JS object.

retrieve.retrieveAllStores(function(data){
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write(JSON.stringify(data));
    res.end();
});

Upvotes: 1

Related Questions