通过NodeJS请求模块获得响应

时间:2017-07-12 23:04:41

标签: json node.js api amazon-web-services twitch

我刚开始使用twitch kraken api,我有几个问题。

每当我尝试获取JSON对象时,都没有响应。我试图通过Amazon AWS Lambda运行此功能,并且无法访问控制台。

在下面的代码中,我的回调函数将始终打印出“SUCCESS got streamers ERROR”。我很确定现在“错误”来自我最初的结果设置。

为什么结果不会变成正确的JSON?

我使用了邮递员,它使用查询和参数以及标题返回正确的内容:

function getJSON(callback){
    var result = "ERROR";
    request.get(url(games[0]),function(error,response,body){
        console.log("requested for url: " + url(games[0]));
        var d = JSON.parse(body);
        result = d.streams[0];//.channel.display_name;
        // for(var i = 0; i < limit; i++){
        //     streamers.push(d.streams[i].channel.display_name)
        // }
        streamers.push(result);
    });
    if (streamers.length < 0){
        callback("ERROR");
    }else{
        callback("SUCCESS got streamers " + result);
    }
}

function url(game){
    return {
        url: "https://api.twitch.tv/kraken/streams/",//twitchlimit,
        qs : {
            'game' : 'overwatch',
            'limit' : 2
        },
        headers: {
            'Client-ID': clientID,
            'Accept': 'application/json',
            'Accept-Charset': 'utf-8',
        }
    };
}

2 个答案:

答案 0 :(得分:1)

我认为你的流媒体代码

if (streamers.length < 0){
    callback("ERROR");
}else{
    callback("SUCCESS got streamers " + result);
}

应该包含在请求回调中,因为目前它还没有等待请​​求完成,它只是继续进行,因此result的值不会改变。此外,数组长度不能小于0,因此它总是会转到其他地方,然后说&#34; SUCCESS得到了飘带ERROR&#34;

答案 1 :(得分:0)

谢谢你们的建议。我确实有一些疏忽并试图修复它们。 我已经实现了你的建议,似乎有点工作。我最终把json.parse放到了try / catch块中,并在getJSON方法中移动了if / else语句。但是,现在我没有任何输出。

这就是我调用getJSON方法的方法:

function handleGameResponse(intent,session,callback){
    //gets the game
    var game = intent.slots.game.value;
    if (!games.includes(game)){
        var speechOutput = "You asked for: " + intent.slots.game.value;
        //var speechOutput = "You asked for: " + games[game] + " That game is currently not an option. These are your current options: " + arrayToString(games)
        var repromptText = "Please ask one from the current options.";
        var header = "Invalid Game";

    }else {
        getJSON(function(data){
            if(data !== "ERROR"){
                var speechOutput = data; //capitalizeFirst(game) + " top three streamers are: " + arrayToString(streamers) + '.';
                var repromptText = "Do you want to hear more about games?";
                var header = capitalizeFirst(game);
            }else{
                var speechOutput = "I'm sorry, something went wrong and I could not get the streamers.";
            }
            //speechOutput = data;
        });
        //speechOutput = games[0] + " games[0], game= " + game; //this executes so the getJSON isn't executing
    }
    var shouldEndSession = false;
    callback(session.attributes,buildSpeechletResponse(header,speechOutput,repromptText,shouldEndSession));
}

上述执行方式是否相同?就像在getJSON有时间给出响应之前执行的shouldEndSession和callback一样?

对于ref,这是getJSON方法:

function getJSON(callback){
    var result = "ERROR";
    request.get(url(games[0]),function(error,response,body){
        try{
            var d = JSON.parse(body);
        } catch (err){
            callback("Sorry, something seems to have malfunctioned while getting the streamers");
        }
        result = d.streams[0].channel.display_name;
        // for(var i = 0; i < limit; i++){
        //     streamers.push(d.streams[i].channel.display_name)
        // }
        streamers.push(result);
        if (streamers.length <= 0){
            callback("ERROR");
        }else{
            callback("SUCCESS got streamers " + result);
        }
    });
}