Leverage WooCommerce API in conjunction with Express.js

Here's the code snippet I've been working on:

const express = require('express');
    const router = express.Router();
    const WooCommerceAPI = require('woocommerce-api');
    const WooCommerce = new WooCommerceAPI({
      url: 'https://example.ro',
      consumerKey: 'ck_xxxxxxxxxxxxxxxxx',
      consumerSecret: 'cs_xxxxxxxxxxxxxxxx',
      wpAPI: true,
      version: 'wc/v1'
    });
    router.get('/', function(req, res) {
        WooCommerce.get('products', function(err, data, response) {
        console.log(response);    
        }); 
    });

    module.exports = router;

Despite receiving a console log in the terminal, I'm struggling to display the JSON data in the route itself. I attempted using res.json(response) within the WooCommerce.get function, but encountered an error indicating that res.json is not a function.

Answer №1

I was able to make this solution work successfully.

const express = require('express');
    const router = express.Router();

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



    const WooCommerce = new WooCommerceAPI({
      url: 'https://example.ro',
      consumerKey: 'ck_xxxxxxxxxxx',
      consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxx',
      wpAPI: true,
      version: 'wc/v1'
    });
    let response;
    const link = 'products'
    router.get('/product', function(req, res) {  

     WooCommerce.get(link,function (err, data, res) {
         response = res;     
    });
    res.json(JSON.parse(response));
    });
    module.exports = router;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the Express server to return an error despite everything being in order?

I am new to exploring express.js and attempting to create a basic sign-in example server. I have set up a database object with a users array containing objects, each with email and password properties. Despite using body-parser to convert the body into a j ...

Is the term "filter" considered a reserved keyword in Angular, Javascript, or ASP.Net MVC?

When using angularJS to call an ASP.Net MVC controller from a service, I encountered an issue with one of the parameters: $http({ method: "get", url: "ControllerMethod", params: { param1: param1Value, pageNumber: pageNumber, ...

Firestore - Using arrayUnion() to Add Arrays

Is there a way to correctly pass an array to the firebase firestore arrayUnion() function? I encountered an issue while attempting to pass an array and received the following error message: Error Error: 3 INVALID_ARGUMENT: Cannot convert an array value ...

Having trouble with a basic select tag and options not functioning properly in Chrome browser

I encountered an issue where I am unable to expand a simple select tag in Chrome. <select id="filterCategory" class=""> <option>1</option> <option>2</option> <option>3</option> <option>4</option ...

The most effective method for transferring asynchronous data to pages in Next.js

My current directory structure: - components - NavBar - Header - Layout - pages - pages - demo.js - _app.js - index.js // index.js import React from 'react'; import NewLayout from "../../components/NewLayout/NewLayou ...

" Jquery.ajax, PHP, and the case of the mysteriously missing response

I've been struggling to create my first universal code for handling ajax responses. Unfortunately, I haven't been able to see any output either in PHP or in the ajax response. There must be something missing in the post data. Below is the ajax r ...

Loop through the array while handling a promise internally and ensure completion before proceeding

I am currently working on populating a response array with Firestore snapshots and creating download links for stored files within each snapshot. Despite trying various solutions involving Promises, the response array consistently ended up being null. do ...

Personalized dropdown appearance

During my work on a select box, I discovered that custom styling is not possible. I attempted to use some plugins but did not find one that met my needs. I am seeking a dropdown menu with options in both black and gray, similar to this template but using ...

Obtaining the Span value inside a DIV when the text in the DIV is selected - JavaScript

I am dealing with a series of div elements structured in the following way: <div id="main1"> <span class="username">Username_1</span> white some text content inside div... blue some text content inside div... yellow some ...

I am converting a class component to a functional component within a React-Redux-Firebase project

I am currently in the process of rebuilding this component. Check out the updated code here Also, take a look at the project actions script here However, I'm facing an issue with rewriting mapStateToProps and mapDispatchToProps functions. The error ...

Having trouble with the Express.js app.post request functionality

I'm facing a challenge with the Express.js library. I've been attempting to set up a basic post request with the route. I have body-parser installed and am using it for the post data. If I specifically include app.post, it doesn't work, ...

Disabling GPS with HTML5/phonegap: A step-by-step guide

Looking to create a function that can toggle GPS on and off for both iPhone and Android devices ...

What is the best way to save a list of items without internet connection when the data is retrieved from a server?

Question: I am facing an issue with my listview that contains 4 items - 3 texts and 1 image. The data is retrieved from a server in JSON format. When the internet is disconnected, the list does not display any items. How can I make sure the list works bo ...

Looking to update the background color of a div every 3 seconds?

Every 3 seconds, I need to change the background color of each div. To do this, I attempted to modify the values in the color array every 3 seconds. For example, the value at index 0 (which is "red") would move to index 1, then the value at index 1 would ...

jquery mobile popup message will fade away within a short time interval

In my main page, I have the following code: $(document).bind("pageinit", function () { $.mobile.loading('hide'); }); I am displaying a popup message using the following code: $.mobile.loading('show&apos ...

There was a hiccup in the camera positioning as the tweening began

I've been working on a basic program that involves tweening the camera to a specific position. The functionality works smoothly for the most part, however, I encounter some glitches at the start of the transition when trying to pan the camera using or ...

Tips for verifying the HTML5 date format

I am looking to implement the HTML5 date input field <input type='date' name='customDate'> This will allow other users to utilize the built-in datepicker available in their browser. One challenge I have encountered is ensuring ...

Error: The function stripHtml cannot be found

Currently, I am developing a blog website using Next Js. I encountered an issue while creating a rich text-editor API for the blog. When attempting to utilize string-strip-html, an error message was displayed as follows: C:\Users\alami\OneDr ...

Is there a way to transform a JSON array into a dictionary that can store duplicate keys as well?

I've been attempting to convert my JSON array into key/value pairs in a Dictionary, but I keep encountering the issue of receiving null Keys and Values. Error : System.ArgumentNullException: 'Value cannot be null. Parameter name: key' What ...

express-flash-notification displays the following error message: "No default engine was specified and no extension was provided."

Recently, I started working with node.js and using MEAN stack for my project. In the footer of my project, I have an option to "Subscribe email". Due to constraints in the architecture, I am unable to utilize Angular for displaying flash messages as I don& ...