Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 功能不带承诺返回_Javascript_Node.js_Promise - Fatal编程技术网

Javascript 功能不带承诺返回

Javascript 功能不带承诺返回,javascript,node.js,promise,Javascript,Node.js,Promise,我试图从另一个函数调用一个函数(refresh\u access\u token),并由此创建一个承诺链。但是刷新\访问\令牌内的返回函数不起作用。刷新\u访问\u令牌完成后不会返回到其调用者。 我收到这个消息: Unhandled rejection TypeError: Cannot read property 'then' of undefined 我怎样才能解决这个问题 以下是2个功能代码: exports.refresh_access_token = function(environ

我试图从另一个函数调用一个函数(refresh\u access\u token),并由此创建一个承诺链。但是刷新\访问\令牌内的返回函数不起作用。刷新\u访问\u令牌完成后不会返回到其调用者。 我收到这个消息:

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

                    });

                }));          
          });
}

您的函数
refresh\u access\u token
没有返回任何内容。您唯一的返回语句位于
指针.post
回调中。您应该首先返回:

return MercadoLibre.findOne(...);
但是,您正在将承诺与回调(用于您的needle.port调用)混合在一起。我建议回顾一下承诺和回调,以及如何将它们结合起来使用。下面是一个关于如何将回调API转换为承诺的好线程:

另一种选择是用promise支持的节点库替换使用
needle


“未经处理的拒绝”?它真的这么说吗?这基本上是一个NPE。您有一个未定义的值。调试以找出原因。