Search code examples
node.jsexpresspipedrive-api

Not able to get callback to work


I am building an API using ExpressJS, NodeJS

The issue is when I call my API using Postman, I do not get any returned results. I do not know how to make Postman wait for the function to return the allproduct result. I am using callback but it is just not working, I have tried many simple callback codes in my Services part of the code but none of them work. Only Async Await makes the Postman API stop and wait for result, however i am using a third party API called Pipedrive, which works only with callback. If i can somehow make the Pipedrive API work with Async/Await it might solve my issue

Route:

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;

Controller

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});
    }


}

Service:

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)


}

Solution

  • First, no need tu put async before the operation function, you need to wrap your service in a promise, you can do something like this :

    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);
              });
          }