Bluebird - TypeError:无法读取属性'然后'未定义的

时间:2018-06-05 21:37:46

标签: javascript node.js aws-sdk bluebird

你能帮我解决这个问题,因为即使我从第一个承诺中归还了价值,我也不知道我在这里失踪了什么吗?我正在使用AWS SDK for Node.js.此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
$

2 个答案:

答案 0 :(得分:3)

您没有从describeSnapshots()返回任何内容。

添加返回:

return describeRegionsPromise.then((data) => {

答案 1 :(得分:1)

您还需要返回describeRegionsPromise.then(),否则regions将被退回:

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);
  });
};