Bluebird - TypeError: Cannot read property 'then' of undefined

Bluebird - TypeError: Cannot read property 'then' of undefined

你能帮我解决这个问题吗,因为我不知道我在这里遗漏了什么,即使我正在返回我第一个承诺的价值?我正在为 Node.js 使用 AWS SDK。此 SDK 支持 Bluebird.

这是我的代码:

const AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));

AWS.config.update({ region: 'us-east-1' });
const ec2 = new AWS.EC2();

const describeRegions = function dr() {
  const describeRegionsPromise = ec2.describeRegions().promise();
  const regions = [];

  describeRegionsPromise.then((data) => {
    data.Regions.forEach((region) => {
      regions.push(region.RegionName);
    });
    return regions; // should return the values to describeSnapshots
    // console.log(regions); // this works! prints the list of AWS regions e.g. us-east-1, ap-southeast-1
  }).catch((err) => {
    console.log(err);
  });
};

const describeSnapshots = function ds1(regions) {
  console.log('Hello I am describeSnapshots!');
  console.log(regions); // should print regions from describeRegions
};

const start = function s() {
  describeRegions()
    .then(regions => describeSnapshots(regions));
};

start();

这是错误:

$ node test.js
/Users/sysadmin/Desktop/test/test.js:29
    .then(regions => describeSnapshots(regions));
    ^

TypeError: Cannot read property 'then' of undefined
    at s (/Users/sysadmin/Desktop/test/test.js:29:5)
    at Object.<anonymous> (/Users/sysadmin/Desktop/test/test.js:32:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
$

您没有 return 从 describeSnapshots().

获取任何内容

为此添加return:

return describeRegionsPromise.then((data) => {

您还需要 return describeRegionsPromise.then() 否则 regions 不会 returned:

const describeRegions = function dr() {
  const describeRegionsPromise = ec2.describeRegions().promise();
  const regions = [];

  return describeRegionsPromise.then((data) => {
    data.Regions.forEach((region) => {
      regions.push(region.RegionName);
    });
    return regions; // should return the values to describeSnapshots
    // console.log(regions); // this works! prints the list of AWS regions e.g. us-east-1, ap-southeast-1
  }).catch((err) => {
    console.log(err);
  });
};