【发布时间】:2020-01-03 04:56:06
【问题描述】:
我正在使用 node.js 和 lambda 服务器编写自己的 Alexa Skill。我的 Alexa 自定义技能从网站获取一个 zip 文件并将该 zip 文件上传到谷歌驱动器。 为此,我使用了一个异步函数,它等待来自 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);
}
}
);
}`
【问题讨论】:
-
不是一个真正的 Alexa 问题。但是您是否记录了任何错误?如果您要部署到 Lambda,请检查您的 CloudWatch 日志。此外,尝试将
const buffer = await getFile()包装在 try/catch 中,然后在 catch 中记录任何错误 - 可能会对您有所帮助。
标签: node.js async-await aws-lambda alexa