0

我在 lambda 函数内调用 REST API 端点,根据返回值生成不同的返回对象。我面临的问题是何时http.request()调用它不会触发主体,结束甚至错误方法。

这是我的代码:

var http = require('http');
function getJSON(options, callback){

    http.request(options, function(res){
        var body = "";
        console.log('calling the http');
        res.on('body', function(chunk){
            console.log('body' + chunk);
            body+=chunk;
        });
        res.on('end', function(){
            console.log('end event');    
            var result = JSON.parse(body);
            callback(null, result);
        })

        res.on('error', function(error){
            console.log('Error event');    
            callback('error', callback);
        })
    })
    .on('error', callback)
    .end();    
}

function getCityWeather(cityName, outputSessionAttributes){

    var options = {
        host: `api.openweathermap.org`,
        port: 80,
        path: `/data/2.5/weather?q=${cityName}&appid=api_key_here`,
        method: 'GET'
    };

    getJSON(options, function(err, result){
        if(err){
            console.log(err);
            return buildValidationResult(false, 'TodayWeatherCity', `Invalid city name. Please let me know the city again.`);
        }
        outputSessionAttributes.temprature = result.main.temp;
        console.log(outputSessionAttributes.temprature + ' value');
        return buildValidationResult(true, null, null);
    });

function getWeatherUpdate(intentRequest, callback) {
    const country = intentRequest.currentIntent.slots.TodayWeatherCountry;
    const city = intentRequest.currentIntent.slots.TodayWeatherCity;
    const source = intentRequest.invocationSource;
    const outputSessionAttributes = intentRequest.sessionAttributes || {};

    console.log("outputSessionArribute", intentRequest.sessionAttributes);

    if (source === 'DialogCodeHook') {
        const slots = intentRequest.currentIntent.slots;


        //without promiss implemeation
        var validationResult = getCityWeather(city, outputSessionAttributes);  
        if(!validationResult.isValid) {
            console.log('after calling getCityWeather with result');
            slots[`${validationResult.violatedSlot}`] = null;
            //if response not found then return the invalid city message
            callback(elicitSlot(intentRequest.sessionAttributes, intentRequest.currentIntent.name, slots, validationResult.violatedSlot, validationResult.message));
            return;
        }
        console.log('getWeatherUpdate after calling getCityWeather');
        callback(delegate(outputSessionAttributes, slots));
        return;
    }

    console.log('getWeatherUpdate after DialogCodeHook');
    if(outputSessionAttributes.temprature){

        console.log('getWeatherUpdate inside outputSessionAttributes.temprature return');
        //get the value from the session variable and prompt to user
        callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText',
        content: `Okay, temprature reading for ${city} is ${outputSessionAttributes.temprature}` }));
    }

    //get the value from the session variable and prompt to user
    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText',
    content: `Sorry, I couldn't server your request` }));
}

getCityWeather被调用时它调用getJSON. 但是一旦它调用getJSON调用 http被打印并且函数返回错误。

4

1 回答 1

1

这个:

res.on('body', ...)

应该是这样的:

res.on('data', ...)

相关信息在这里

但是,如果'response'添加了事件处理程序,则必须使用来自响应对象的数据,或者通过response.read()在有'readable'事件时调用,或者通过添加'data'处理程序,或者通过调用.resume()方法。在消耗数据之前,该'end'事件不会触发。此外,在读取数据之前,它将消耗内存,最终可能导致“进程内存不足”错误。

因此,由于您没有添加data处理程序,因此响应流保持暂停。

于 2017-05-25T17:49:39.997 回答