自定义技能不会执行提供承诺的异步功能

时间:2019-08-29 22:03:15

标签: node.js async-await aws-lambda alexa

我正在使用node.js和lambda服务器编程自己的Alexa技能。我的Alexa自定义技能从网站获取了一个zip文件,并将该zip文件上传到Google驱动器。 为此,我使用一个异步函数,该函数等待getFile()函数中的Promise对象。 该代码在本地运行,因此我认为lambda函数无法处理我的异步调用。

我在没有异步和等待的情况下实现了它,但是随后收到错误消息:

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object.


function getFile() {
  return new Promise(function(resolve, reject) {

   //from here on nothing is executed anymore

    request(
      {
        method: "GET",
        url: "https://start.spring.io/starter.zip",
        encoding: null // <- this one is important !
      },
      function(error, response, body) {
        if (error && response.statusCode != 200) {
          reject(error);
          return;
        }
        resolve(body);
      }
    );
  });
}

 async function uploadFile(auth) { 

  const stream = require("stream"); 

  const buffer = await getFile(); // from here on nothing is executed anymore


  const bs = new stream.PassThrough();
  bs.end(buffer);

  const drive = google.drive({ version: "v3", auth });
  var fileMetadata = {
    name: "demo.zip"
  };
  var media = {
    mimeType: "application/zip",
    body: bs // Modified
  };
  drive.files.create(
    {

      resource: fileMetadata,
      media: media,
      fields: "id"
    },
    function(err, res) {
      if (err) {
        // Handle error
        console.log(err);
      } else {
        console.log("File Id: ", res.data.id);
      }
    }
  );
}`

1 个答案:

答案 0 :(得分:0)

此外,请确保您的lambda运行时为节点8.10。节点4.3不支持Async / Await

Function Code Runtime