Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
Node.js 将Alexa与其他API一起使用_Node.js_Api_Alexa - Fatal编程技术网

Node.js 将Alexa与其他API一起使用

Node.js 将Alexa与其他API一起使用,node.js,api,alexa,Node.js,Api,Alexa,我正在努力发展Alexa技能,这将找到用户所说单词的示例句子。我找到了这个API(WordAPI),尽管当我进行调用时,响应是未定义的。有人能帮忙吗 我的代码: 'use strict'; var Alexa = require('alexa-sdk'); var appId = 'this is valid'; var unirest = require('unirest'); var APP_STATES = { START: "_STARTMODE", TRANSLATE

我正在努力发展Alexa技能,这将找到用户所说单词的示例句子。我找到了这个API(WordAPI),尽管当我进行调用时,响应是未定义的。有人能帮忙吗

我的代码:

'use strict';
var Alexa = require('alexa-sdk');
var appId = 'this is valid';
var unirest = require('unirest');

var APP_STATES = {
    START: "_STARTMODE",
    TRANSLATE: "_TRANSLATE"
}

function getData(word){
    unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
    .header("X-Mashape-Key", "my key")
    .header("Accept", "application/json")
    .end(function (result) {
        return JSON.parse(result.body);
    });
}

exports.handler = function(event, context, callback){
    var alexa = Alexa.handler(event, context);
    alexa.appId = appId;
    alexa.registerHandlers(newSessionHandlers, startStateHandler, translateStateHandler);
    alexa.execute();
}

var newSessionHandlers = {
    'LaunchRequest': function(){
        this.handler.state = APP_STATES.START;
        this.emitWithState("BeginState", true);
    },

    'Unhandled': function () {
        this.emit(":tell", "Something went wrong");
    },
}

var startStateHandler = Alexa.CreateStateHandler(APP_STATES.START, {
    'BeginState': function(){
        var message = "You will say a word and I will give you facts about it, would you like to continue ?";
        this.emit(":ask", message, message);
    },

    'AMAZON.YesIntent': function(){
        this.handler.state = APP_STATES.TRANSLATE;
        this.emit(":ask", "Great, say a word !");
    },

    'AMAZON.NoIntent': function(){
        this.emit(":tell", "Ok, see you later !");
    },

    'Unhandled': function () {
        this.emit(":tell", "Something went wrong");
    },
});

var translateStateHandler = Alexa.CreateStateHandler(APP_STATES.TRANSLATE, {
    'GetWordIntent': function(){
        var word = this.event.request.intent.slots.word.value;
        console.log(getData(word));
        this.emit(":tell", "You said " + word);
    },

    'Unhandled': function () {
        this.emit(":tell", "Something went wrong");
    },

});
在尝试console.log函数时出现问题。它返回undefined

    'GetWordIntent': function(){
      var word = this.event.request.intent.slots.word.value;
      console.log(getData(word));
      this.emit(":tell", "You said " + word);
    },
原始函数应该返回调用中解析的数据

function getData(word){
  unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
  .header("X-Mashape-Key", "my key")
  .header("Accept", "application/json")
  .end(function (result) {
    return JSON.parse(result.body);
  });
}
这正处于开发的早期阶段,我正在尝试console.log输出。这可能是我看不出来的愚蠢的错误。我替换了appId和API密钥。API起作用了,我在其他场景中检查了它


任何提示或提示都将不胜感激。

您不会从
getData
函数返回任何值

试一试


这在目前的形式中是非常不清楚的。你能指出什么电话出了问题吗,什么是未定义的,在哪里?当然,给我一点时间。我将编辑原始帖子。
function getData(word){
    return unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
    .header("X-Mashape-Key", "my key")
    .header("Accept", "application/json")
    .end(function (result) {
        return JSON.parse(result.body);
    });
}