Bluebird - TypeError: Невозможно прочитать свойство 'then' из неопределенного - PullRequest
0 голосов
/ 06 июня 2018

Можете ли вы помочь мне понять это, потому что я не знаю, чего мне здесь не хватает, хотя я возвращаю значение из моего первого обещания?Я использую AWS SDK для 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 голосов
/ 06 июня 2018

Вам также необходимо вернуть 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);
  });
};
0 голосов
/ 06 июня 2018

Вы ничего не возвращаете с describeSnapshots().

Добавьте возврат к этому:

return describeRegionsPromise.then((data) => {
...