简体   繁体   中英

Use woocommerce api with express.js

This is my code.

var express = require('express');
    var router = express.Router();
    var WooCommerceAPI = require('woocommerce-api');
    var 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, res) {
        console.log(res);    
        }); 
    });

    module.exports = router;

And i cant seem to find a solution to print the json to the route. I do get a the console log in the terminal. I tried res.json(res) inside WooCommerce.get but i get an error(res.json not a function).

This is the solution i got working.

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

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



    var WooCommerce = new WooCommerceAPI({
      url: 'https://example.ro',
      consumerKey: 'ck_xxxxxxxxxxx',
      consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxx',
      wpAPI: true,
      version: 'wc/v1'
    });
    var response;
    var 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM