使用woocommerce api和express.js

时间:2017-03-23 17:18:09

标签: javascript json wordpress express woocommerce-rest-api

这是我的代码。

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;

我似乎无法找到将json打印到路线的解决方案。我确实在终端上获得了一个控制台日志。 我在WooCommerce.get中尝试res.json(res)但是我收到错误(res.json不是函数)。

1 个答案:

答案 0 :(得分:1)

这是我工作的解决方案。

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;