Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Javascript 向回调函数传递dynamodb响应数据时出现的问题_Javascript_Node.js_Aws Lambda_Amazon Dynamodb - Fatal编程技术网

Javascript 向回调函数传递dynamodb响应数据时出现的问题

Javascript 向回调函数传递dynamodb响应数据时出现的问题,javascript,node.js,aws-lambda,amazon-dynamodb,Javascript,Node.js,Aws Lambda,Amazon Dynamodb,朋友们,我的头一直撞在桌子上。我正在写一个Alexa+Lambda+Dynamodb动作/事件串。问题出现在我的Lambda Node.JS中。我有一个函数gethorses,在这个函数中,我试图运行一个简单的getItem来提取dynamodb中的一个示例条目,但是我只能访问Else语句中返回的数据,但是我需要通过speechOutput中的回调传递它。如果您能提供任何帮助,我们将不胜感激 function getChores(callback) { sessionAttributes

朋友们,我的头一直撞在桌子上。我正在写一个Alexa+Lambda+Dynamodb动作/事件串。问题出现在我的Lambda Node.JS中。我有一个函数gethorses,在这个函数中,我试图运行一个简单的getItem来提取dynamodb中的一个示例条目,但是我只能访问Else语句中返回的数据,但是我需要通过speechOutput中的回调传递它。如果您能提供任何帮助,我们将不胜感激

function getChores(callback) {
    sessionAttributes = {};

    var params = {
        TableName: 'Chores',
        Key: {
            'chore': {
                S: 'Clean up toys'
            },
        }
    };

    // Call DynamoDB to read the item from the table
    var results = ddb.getItem(params, function(err, data) {
        if (err) {
            console.log("Error", err);
        } else {
            //CAN ONLY ACCESS HERE!
            console.log("Success", data.Item);
            speechOutput = data.Item.chore.S;
            console.log(data.Item.chore.S);
            console.log(speechOutput);
        }

    });

    console.log(results);

    //Get card title from data
    const cardTitle = "Chore"

    //Get output from data
    //const speechOutput = element.chore;
    // If the user either does not reply to the welcome message or says something that is not
    // understood, they will be prompted again with this text.
    const repromptText = '';
    const shouldEndSession = false;
    callback(sessionAttributes,
        buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

看起来ddb.getItem是一个异步方法。您需要在else块内调用回调方法callback