skip to Main Content

I’m trying to start work with WooCommerce API – Node.js Client, which looks very straightforward.

But when I copy a simple example from WooCommerce official website, I get the following error:

TypeError: WooCommerce.get(...).then is not a function

Here is the code:

var WooCommerceAPI = require('woocommerce-api');

var WooCommerce = new WooCommerceAPI({
  url: 'https://somewebsite.com/',
  consumerKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  consumerSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  wpAPI: true,
  version: 'wc/v1'
});

WooCommerce.get("products/1359")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });

https://woocommerce.github.io,
NPM package

3

Answers


  1. Chosen as BEST ANSWER

    Have found this at the bottom of the NPM package page:

    Every method can be used in a promified way just adding Async to the method name. Like in:

    WooCommerce.getAsync('products').then(function(result) {
      return JSON.parse(result.toJSON().body);
    });
    

    Which actually makes the code works now.

    I still want to understand what I was doing wrong, I don't think that the official API documents website of WooCommerce is showing all code examples as wrong.

    I guess it got to do something with WooCommerce.get not returning a promise, but again that is the way it's in the documents.


  2. Apparently woocommerce.github.io are referring to @woocommerce/woocommerce-rest-api NPM package where docs.woocommerce.com official website refer to WooCommerce API – Node.js Client NPM package.

    Kinda confusing…but that solves the mystery.

    Login or Signup to reply.
  3. I was facing a similar problem but with post method. Solved adding a function callback with response argument.

    WooCommerce.post("orders", data,function(req,res){
        console.log(res.statusMessage);     
    }); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search