2017-07-07 132 views
3

更新:我的http请求端点发生错误。我没有设置适当的认证选项,以便修复很多错误,可能是这个特定的错误。Node.js Lambda函数“响应无效”亚马逊Alexa

我的问题是类似的一个位置:

Node.js Lambda function returns "The response is invalid" back to Alexa Service Simulator from REST call

但是解决这个问题并没有解决我的问题。因此,我向Hana云中的xsjs服务发起http请求调用。我收到'回复无效'的错误讯息。我看不出为什么。这里是我的功能:

// Create a web request and handle the response. 
function httpGet(query, callback) { 

    console.log("/n QUERY: "+ query); 

    var host = 'datacloudyd070518trial.hanatrial.ondemand.com'; 
    var path = '/LocationInformation/getLocationInfo.xsjs?location='; 
    var hostname = 'https://' + host + path + query; 


    var auth = 'user1:D1anafer'; 

    var req = http.request({'hostname': hostname, 
          'auth': auth 
         }, (res) => { 

    var body = ''; 

     res.on('data', (d) => { 
      body += JSON.stringify(d); 
     }); 

     res.on('end', function() { 
      callback(body); 
     }); 

    }); 


    req.end(); 

    req.on('error', (e) => { 
     console.error(e); 
    }); 


} 

并调用它的功能:

'getNewsIntent': function() { 

    //self = this; 

    httpGet(location, function (response) { 

     // Parse the response into a JSON object ready to be formatted. 
     //var output = JSON.parse(response); 
     //output = output['change']; 
     var output = response; 

     var cardTitle = location; 
     var cardContent = output; 

     alexa.emit(':tellWithCard', output, cardTitle, cardContent); 

    }); 

}, 

谢谢 -Diana

回答

2

内,您的AWS帐号去你Lambda功能,点击monitoring标签,您应该在右侧看到“在Cloudwatch中查看日志”。如果您单击该链接并且您应该看到正在生成的错误。

您还可以使用console.log()来记录您的REST API返回的任何信息,这些信息将记录在cloudwatch中,并可帮助您查看错误发生的位置。

0

这只是我头顶的猜测。要真正帮助一些详细的错误消息将需要提到有关。

但只是一个猜测:您的http.request()使用http modulehttps://nodejs.org/api/http.html),并且您正在访问https资源。如果是这样,有一个https(https://nodejs.org/api/https.html)模块或使用类似axios https://www.npmjs.com/package/axios或requestjs(https://github.com/request/request)这将处理这两个。

就像我刚才说的一个没有详细错误信息的盲目猜测,看到你的需求陈述,但如果你碰巧有细节,我很乐意深入。

HTH

0

你的回调从LAMBDA必须返回一个有效的状态代码和身体。就像这样:

let payload = { 
    statusCode: 400, 
    body: JSON.stringify('body'), 
    headers: {"Access-Control-Allow-Origin": "*"} 
}; 
callback(null, payload); 

最重要的是,从客户端代码中调用这个,你必须通过CORS头回来。