无法回调工作

时间:2018-04-10 14:31:53

标签: node.js express pipedrive-api

我正在使用ExpressJS,NodeJS构建API

问题是当我使用Postman调用我的API时,我没有得到任何返回的结果。我不知道如何让Postman等待函数返回allproduct结果。我正在使用回调,但它只是不起作用,我在我的服务部分代码中尝试了许多简单的回调代码,但它们都没有工作。只有Async Await使Postman API停止并等待结果,但是我使用的是名为Pipedrive的第三方API,它只适用于回调。如果我能以某种方式使Pipedrive API与Async / Await一起工作,它可能会解决我的问题

路线:

var express = require('express')

var router = express.Router()

// Push the job to different controller functions
var PipedriveController = require('../../controllers/pipedrive.controller');

router.get('/products', PipedriveController.pipedriveAllProducts)

// Export the router

module.exports = router;

控制器

var PipedriveService = require('../services/pipedrive.service')

// Async Controller Function

exports.pipedriveAllProducts = async function(req, res, next){
    // let family = req.param.options;

    try {
        let all_products = await PipedriveService.pipedriveAllProducts()

        //  Return All product liist with Appropriate HTTP header response
        return res.status(200).json({status: 200, all_products});
    } catch(e){

        // Return an Error Response Message
        return res.status(400).json({status: 400, message: e.message});
    }


}

服务:

var Pipedrive = require('pipedrive');
var pipedrive = new Pipedrive.Client('SECRET', { strictMode: true });

// Saving the context of this module inside the _the variable
_this = this




exports.pipedriveAllProducts = async function operation(options){
    // Array of product object - after which will be converted to JSON
    const allproducts = [];

    function iterateprods (err, products) {
        if (err) throw err;
        for (var i = 0; i < products.length; i++) {
            // console.log(products[i].prices["0"].price);

            let product = {
                "id": products[i].code,
                "name": products[i].name,
                "price": products[i].prices["0"].price
            }

            allproducts.push(product)

        }
        console.log(JSON.stringify(allproducts));
        return allproducts
    }

    pipedrive.Products.getAll({},iterateprods)


}

1 个答案:

答案 0 :(得分:0)

首先,不需要在async函数之前调整operation,您需要将服务包含在承诺中,您可以执行以下操作:

var Pipedrive = require('pipedrive');
var pipedrive = new Pipedrive.Client('SECRET', { strictMode: true });

// Saving the context of this module inside the _the variable
_this = this

exports.pipedriveAllProducts = function operation(options){
// Array of product object - after which will be converted to JSON
const allproducts = [];

return new Promise((resolve, reject) => {
       pipedrive.Products.getAll({}, function(err, products){
                if (err) reject(err);
                for (var i = 0; i < products.length; i++) {
                // console.log(products[i].prices["0"].price);

                let product = {
                    "id": products[i].code,
                    "name": products[i].name,
                    "price": products[i].prices["0"].price
                    }

               allproducts.push(product)

               }
         console.log(JSON.stringify(allproducts));
         resolve(allproducts);
          });
      }