功能不承诺回报

时间:2017-01-23 01:08:13

标签: javascript node.js promise

我试图从另一个函数调用一个函数(refresh_access_token)并从中创建一个Promise链。但是refresh_access_token中的返回函数不起作用。 refresh_access_token在完成时不会返回给它的调用者。 我收到此消息:

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

我该如何解决这个问题?

这是2个功能代码:

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

                    });

                }));          
          });
}

1 个答案:

答案 0 :(得分:1)

您的函数#include <iostream> using namespace std; int main() { //DECLARATIONS int score; int round; int total; int count; double average = 0; // average score of an archer for (round = 0; round < 4; round++) { total = 0; for(count = 0; count < 3;) { cout << "Please enter the Archer's Score' "; cin >> score; if (score<0 || score> 60) { cout << "\nThe value you entered is out of range, Please enter a number between 0 - 60 \n"; } else { count++; total = total + score; } } cout << "Total Score = " << total << endl; average = total / count; cout << "Average Score = " << average << endl; } return 0; } 没有返回任何内容。您唯一的return语句位于refresh_access_token回调中。你应该先回来了:

needle.post

然而,你正在混合承诺,回调(在你的needle.port调用中使用)。我建议审查Promises vs Callback以及如何一起使用它们。以下是关于如何将callback-apis转换为promises的好主题:How do I convert an existing callback API to promises?

另一种替代方法是使用支持promise的节点库替换return MercadoLibre.findOne(...);