> On Jan 25, 2016, at 2:07 AM, naijacoder <[email protected]> wrote:
> 
> I'm trying to list all products in WooCommerce using node.js but cant get to 
> list all the products to the browser.
> When i run the code i only get to see the first record.
> 
> I'm a newbie with node.js
> 
> Thanks in Advance
> 
> //Lets require/import the HTTP module
> var http = require('http');
> 
> //Lets define a port we want to listen to
> const PORT=8080; 
> 
> //We need a function which handles requests and send response
> function handleRequest(request, response){
>     
>     //response.end('It Works!! Path Hit: ' + request.url);
>     
>     var WooCommerceAPI = require('woocommerce-api');
> 
>     // Initialize the WooCommerceAPI class
>     var WooCommerce = new WooCommerceAPI({
>         //url: 'http://example.com', // Your store url (required)
>     });
> 
>     // GET example
>     WooCommerce.get('products', function (err, data, res) {
>         //console.log(res);
>     
>         //var fs = require('fs');
>         //var jsonContent = JSON.parse(JSON.stringify(res, null, 4))
>         var jsonContent = JSON.parse(res)
> 
>         for (var i = 0; i < jsonContent["products"].length; i++)
>         {
>             var name = jsonContent["products"][i];
>              //This works in console
>             //console.log(name['title']);
>             //console.log(name['id']);
>             //console.log(name['sku']);
>             //console.log(name['regular_price']);
>             response.writeHead(200, {'Content-Type': 'text/plain' });
>             response('It Works!! ' + name['id'] + ' ' + name['title']);
>             //response.end('It Works!! Path Hit: ' + name['id'] + ' ' + 
> name['title']);
> 
>         }
>         //response.end('It Works!! Path Hit: ' + name['id']);
>     });
> 
>    //response.end('It Works!! Path Hit: ' + name['id']);
> 
> 
> }

The documentation says you must call writeHead only once per response. You're 
calling it several times, once per product. 
https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers

Then, you're calling response(...) as if it were a function. I'm not familiar 
with that usage. response is actually an object, not a function. Inside the for 
loop, you should call the response.write(...) function to write out each 
product. 
https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback

Alternately, you could build up the data you want to send in a variable, and 
write it to the response all at once.


-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/3874F47F-E53C-4BAF-B5B6-74D0D01D79B7%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to