-1

I'm trying to call a function (refresh_access_token) from another one and create a Promise chain from that. But the return function inside refresh_access_token is not working. The refresh_access_token doesn't return to it's caller when it completes. I'm receiving this message:

Unhandled rejection TypeError: Cannot read property 'then' of undefined

How can I fix this?

These are the 2 function code:

exports.refresh_access_token = function(environment_hash) {

    var MercadoLibre  = require('../../models/index').MercadoLibre;
    var needle = require('needle');
    const load = require('express-load');
    var meli = require('mercadolibre');
    var request=require('request');
    const mysql = require('mysql');
    const dotenv = require('dotenv').config({path: '../../.env'});

    var oauth_url = 'https://api.mercadolibre.com/oauth/token';


    var env_hash = environment_hash;

    where = { environment_hash: env_hash };

    MercadoLibre.findOne({where: where}).then(function (account) {
        if (!account) {
            // Item not found

        } else {
            // Found an item, update it
            needle.post(oauth_url, {
                grant_type: 'refresh_token',
                client_id: process.env.MERCADOLIBRE_CLIENT_ID,
                client_secret: process.env.MERCADOLIBRE_SECRET,
                refresh_token: account.refresh_token
            }, {
            }, function (err, res, body) {
                if (body) {
                        expires_in = new Date();
                        expires_in.setUTCHours(0);
                        expires_in.setTime(expires_in.getTime() + 21600*1000);

                        values = {
                            refresh_token: body.refresh_token,
                            expires_in: expires_in

                        };

                        where = { environment_hash: env_hash };

                        return MercadoLibre.update(values, {where: where});


                }
            });       
        }
    });

}


exports.run_mercadolibre_jobs = function() {

    var MercadoLibre  = require('../../models/index').MercadoLibre;


    var values = {
                attributes: ['environment_hash'],
                raw: true
        };

        MercadoLibre
            .findAll(values)
            .then(function(accounts) {

                Promise.all(accounts.map(function(account) { 

                    module.exports.refresh_access_token(account.environment_hash)
                    .then(function(response) {

                        console.log(response);

                    })
                    .catch(function(response){
                        console.log(response);

                    });

                }));          
          });
}
Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174

1 Answers1

1

Your function refresh_access_token is not returning anything. Your only return statement is inside the needle.post callback. You should be first returning:

return MercadoLibre.findOne(...);

However, you are mixing promises, with callbacks (used in your needle.port call). I would suggest reviewing Promises vs Callbacks and how to use them together. Here is a good thread on how to convert callback-apis to promises: How do I convert an existing callback API to promises?.

Another alternative would be to replace using needle with a promise-supported node library:

Community
  • 1
  • 1
Sanketh Katta
  • 5,961
  • 2
  • 29
  • 30