简体   繁体   English

nodejs异步函数未返回

[英]nodejs async function not returning

I have a function. 我有一个功能。 Inside of this function I check I verify a token, send a http request to Facebook, and another http request to my cloud server. 在此功能内,我检查是否验证了令牌,向Facebook发送一个http请求,以及向我的云服务器发送另一个http请求。

function myAsyncFunction(token,fbat){
    if (token) {
        jwt.verify(token, jwt_secret, {
            issuer: 'me@me.com'
        }, function(err, decoded) {
            if (err) {
                if (err.name === 'TokenExpiredError') {
                    console.log('Token expired'); // renew token

                    var options = {
                        hostname: 'graph.facebook.com',
                        port: 443,
                        //path: '/oauth/?appsecret_proof='+hash+'&access_token='+at,
                        path: '/v2.5/me?fields=id&access_token=' + fbat,
                        method: 'GET'
                    }; //end of options


                    var callback = function(response) {
                        var str = '';

                        //another chunk of data has been received, so append it to `str`
                        response.on('data', function(chunk) {response on;
                            str += chunk;
                        });

                        response.on('end', function() { //start of response end;
                            var json = JSON.parse(str);
                            if (json.hasOwnProperty('id')) {

                                var params = {
                                    TableName: 'Users',
                                    Key: {
                                        'fid': {
                                            'N': json.id.toString()
                                        }
                                    },
                                    ConsistentRead: false,
                                    ProjectionExpression: 'fid,st'
                                };


                                dynamodb.getItem(params, function(err, data) {
                                    if (err) {
                                        return false;
                                    } else { //start of dynamodb else                                       
                                        if (isEmptyObject(data)) {

                                            return false;


                                        }
                                        else {
                                            if (data.Item.st.S === 't') {


                                               return true;

                                            } else {
                                              return false;
                                            }


                                        }


                                    }

                                });



                            }
                            else {
                                return false;
                            };


                        });
                        response.on('error', function() {
                            return false;
                        });

                    };

                    https.request(options, callback).end();

                } else if (err.name === 'JsonWebTokenError') {
                    return false;    
                } else {
                    return false;
                }
            } else {
                return true;   
            }
        });
    } else {
        return false;
    }
    return false;
};

I then try to call this function: 然后,我尝试调用此函数:

myAsyncFunction(token, fbat, function(result){

if(result){
//do some network calls here
}
else{
//do some other network calls here
}

});

When I call the async function I debugged it and it is making the calls to Facebook and to my server, the problem is it is not returning anything. 当我调用异步函数进行调试时,它正在对Facebook和我的服务器进行调用,问题是它没有返回任何内容。 It gets gets to the point of a return statement, but the condition to check if the result is true or false never gets executed. 它到达了return语句的地步,但是检查结果是true还是false的条件永远不会执行。

Your myAsyncFunction only accepts two arguments: function myAsyncFunction(token,fbat) { ... } . 您的myAsyncFunction仅接受两个参数: function myAsyncFunction(token,fbat) { ... } The callback you are passing in when you call can thus never be executed. 因此,在调用时传递的回调将永远无法执行。 Also the various return statements in the asynchronous function won't have the desired effect. 同样,异步函数中的各种return语句也不会达到预期的效果。

I recommend reading up on the callback style of node.js / javascript. 我建议阅读node.js / javascript的回调样式。

What you're ultimately looking for is something like this: 您最终要寻找的是这样的:

function myAsyncFunction(token, fbat, callback) {
    someOtherasyncFunction(arg1, arg2, function(err, result) {
         if (err) {
              return callback(err); // propagate error
         }
         yetAnotherAsyncFunction(argA, argB, function(err, result2) {
              if (err) {
                  return callback(err); // propagate error
              }
              // do something with result2...
              callback(null, myResult); // result2 is the result being passed into the callback
         });
    });
}

Now when you're calling your function: 现在,当您调用函数时:

myAsyncFunction(token, fbat, function(err, result) {
    if (err) {
        // handle error... something has gone wrong...
    }
    // do something with the result...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM