在NodeJS中传递回调函数变成Object

Passing callback function in NodeJS turns into Object

本文关键字:函数 Object 回调 NodeJS      更新时间:2024-04-27

我正在NodeJS中使用AWS Lambda函数制作Alexa技能。

当我调用Intent:时,应用程序抛出错误

"errorMessage": "Exception: TypeError: object is not a function"

首先,我的应用程序获取一个事件。如果是意向,则调用:

exports.handler = function (event, context) {
    try {
           ...
           else if (event.request.type === "IntentRequest") {
             onIntent(
                event.request,
                event.session,
                function intent_callback(sessionAttributes, speechletResponse) {
                    context.succeed(buildResponse(sessionAttributes, speechletResponse));
                }
             );

您可以看到上面向onIntent()传递了一个回调。它检查它是哪个Intent。这里的Console.logging将传递回调显示为function:

function onIntent(intentRequest, session, callback) {
    if ("ItemIntent" === intentName) {
        console.log(callback); // This is a function
        getOrderResponse(intent, session, callback);

然而,getOrderResponse()callback的类型不知何故变成了一个对象?这就是为什么我会得到这个错误,但我不明白它怎么不是function类型。为什么它是一个物体?

function getOrderResponse(callback) {
    console.log('getOrderResponse', callback); // type = Object:  { name: 'ItemIntent', slots: { Item: { name: 'Item' } } }
    var card_title = config.data().CARD_TITLE;
    var sessionAttributes = {},
        speechOutput = 'So you want quick order',
        shouldEndSession = false,
        repromptText = 'Hello';
    sessionAttributes = {
        'speechOutput': repromptText,
        'repromptText': repromptText,
        'questions': 'some questions'
    };
    callback(sessionAttributes, buildSpeechletResponse(card_title, speechOutput, repromptText, shouldEndSession));
}

回调必须是第三个参数。

getOrderResponse(intent, session, callback);您要发送的第一个参数是intent对象。

function getOrderResponse(callback) {

应该是

function getOrderResponse(intent, session, callback) {