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);
});
3
Answers
Have found this at the bottom of the NPM package page:
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.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.
I was facing a similar problem but with post method. Solved adding a function callback with response argument.