这是我的问题:我想在aws-lambda函数(nodejs 12)中调用Vimeo api,以获取有关视频的一些信息/数据(例如:时长,标题等)。
这是我的代码:
exports.handler = async event => {
let Vimeo = require("vimeo").Vimeo;
let client = new Vimeo("{client_id}", "{client_secret}", "{access_token}");
console.log('client => ',
client);
console.log('event => ', event);
video_id = event.video_id;
const res = await client.request(
{
method: "GET",
path: `/users/1234567890/videos/${video_id}`
},
function(error, body, status_code, headers) {
if (error) {
console.log("error", error);
}
console.log("body", body);
console.log("status code");
console.log(status_code);
console.log("headers");
console.log(headers);
return body;
}
)
console.log('hello', res);
return 'ok';
};
要尝试,我进行了一些测试。 lambda返回ok
,所以我知道我的函数会通过所有指令,但console.log返回hello undefined
。
对我来说(我是说我想)是回调,目前我知道如果您等待足够的时间,client.request(...)
会返回良好的值;但是即使使用async function
和await
,lambda看起来也很忙,无法等待vimeo api的响应。
thx,祝您有个美好的一天
答案 0 :(得分:1)
await client.request()
不会返回等待的保证。
您需要这样自己完成:
exports.handler = async event => {
const Vimeo = require('vimeo').Vimeo
const client = new Vimeo('{client_id}', '{client_secret}', '{access_token}')
console.log('client => ', client)
console.log('event => ', event)
const videoId = event.video_id
const res = await new Promise((resolve, reject) => {
client.request({
method: 'GET',
path: `/users/1234567890/videos/${videoId}`
},
function (error, body, statusCode, headers) {
if (error) {
console.log('error', error)
reject(error)
return
}
console.log('body', body)
console.log('status code')
console.log(statusCode)
console.log('headers')
console.log(headers)
resolve(body)
}
)
})
console.log('hello', res)
return 'ok'
}