Help with node.js and WooCommerce -

390 views
Skip to first unread message

naijacoder

unread,
Jan 26, 2016, 11:37:48 PM1/26/16
to nodejs
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']);


}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!


Duy Nguyen

unread,
Jan 28, 2016, 10:06:43 AM1/28/16
to nod...@googlegroups.com
When you said you just see 1 object, mean for loop just run 1 time? How about res object from WooCommerce api? Can you log it to console to see how many items it return originally?

--
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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/f5fef765-b6b2-43e1-b459-f47c1624c805%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Nguyen Hai Duy
Mobile : 0914 72 1900
Skype: nguyenhd2107

Ryan Schmidt

unread,
Feb 11, 2016, 1:13:18 PM2/11/16
to nod...@googlegroups.com
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.


Reply all
Reply to author
Forward
0 new messages