Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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
Aws lambda AWS Lambda技能处理程序在数据库获取完成之前返回_Aws Lambda_Alexa - Fatal编程技术网

Aws lambda AWS Lambda技能处理程序在数据库获取完成之前返回

Aws lambda AWS Lambda技能处理程序在数据库获取完成之前返回,aws-lambda,alexa,Aws Lambda,Alexa,我已经写了我的第一个Lambda来处理Alexa技能 我的问题是对数据库的调用显然是异步的(我可以从Console.log消息在云日志中出现的顺序看出) 这是我的经纪人。 如何使返回发生在从数据库获取数据之后 const RemindMeHandler = { canHandle(handlerInput) { const request = HandlerInput.requestEnvelope.request; return request.type === 'L

我已经写了我的第一个Lambda来处理Alexa技能

我的问题是对数据库的调用显然是异步的(我可以从Console.log消息在云日志中出现的顺序看出)

这是我的经纪人。 如何使返回发生在从数据库获取数据之后

const RemindMeHandler = {
   canHandle(handlerInput) {
     const request = HandlerInput.requestEnvelope.request;
     return request.type === 'LaunchRequest'
       || (request.type === 'IntentRequest'
          && request.intent.name === 'RemindMeIntent');
},
handle(handlerInput) {

   console.log('Started Reminder');

   var thing="Nothinbg";

/* ========== Read dB ========== */

   const params = 
   {
       TableName: 'ItemsToRecall',
       Key: {
          'Slot': {S: '1'}
         },
   };


   readDynamoItem(params, myResult=>
   {
      console.log('Reminder Results:  ' + myResult.data);

      thing="Captain";
      console.log('thing 1:  ' + thing);   
   });

   console.log('Ended Reminder');

   function readDynamoItem(params, callback) 
   {

       var AWS = require('aws-sdk');
       AWS.config.update({region: 'eu-west-1'});

       var docClient = new AWS.DynamoDB();

       console.log('Reading item from DynamoDB table');

       docClient.getItem(params, function (err, data) 
       {
          if (err) {
              callback(err, data);
           } else {
              callback('Worked', data);
           }
       });
}

/* ========== Read dB End ========== */
console.log('thing 2:  ' + thing);
return handlerInput.responseBuilder
  .speak(REMINDER_ACKNOWLEDGE_MESSAGE + thing)
  .getResponse();

}
};
/* ========== Remind Handler End  ========== */

您可以包装异步并返回承诺,然后使用async/await语法获取数据。您可以检查以下内容。请注意,它未经测试

const RemindMeHandler = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "LaunchRequest" ||
      (handlerInput.requestEnvelope.request.type === "IntentRequest" &&
        handlerInput.requestEnvelope.request.intent.name === "RemindMeIntent")
    );
  },
  async handle(handlerInput) {
    console.log("Started Reminder");
    let thing = "Nothinbg";
    const params = {
      TableName: "ItemsToRecall",
      Key: {
        Slot: { S: "1" }
      }
    };
    const data = await readDynamoItem(params);
    console.log("Reminder Results:  ", data);
    thing = "Captain";
    let speechText = thing;
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .getResponse();
  }
};

function readDynamoItem(params) {
  const AWS = require("aws-sdk");
  AWS.config.update({ region: "eu-west-1" });
  const docClient = new AWS.DynamoDB();
  console.log("Reading item from DynamoDB table");
  return new Promise((resolve, reject) => {
    docClient.getItem(params, function(err, data) {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

成功了,助教