Node.js “Lambda节点”;“你好,未定义”;异步/等待don';不行?

Node.js “Lambda节点”;“你好,未定义”;异步/等待don';不行?,node.js,asynchronous,callback,aws-lambda,vimeo-api,Node.js,Asynchronous,Callback,Aws Lambda,Vimeo Api,这是我的问题:我想在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(

这是我的问题:我想在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
之外,所有指令都会执行

对我来说(我的意思是我猜),这是回调,目前我100%知道
客户端。如果等待足够的时间,请求(…)
返回正确的值;但是,即使使用
异步函数
等待
,lambda看起来也太忙,无法等待vimeo api的响应

thx and have a good day

等待客户端。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'
}

wait 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'
}


太多了,这是工作!正如你所说,我们需要承诺等待!太多了,这是工作!正如你所说,我们需要承诺等待!