你能帮我弄清楚这一点吗,因为我不知道我在这里遗漏了什么,即使我返回了我第一个承诺的值?我正在使用AWS SDK for Node.js。该软件开发工具包支持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
$
发布于 2018-06-06 05:40:03
还需要返回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);
});
};
发布于 2018-06-06 05:39:35
您不会从describeSnapshots()
返回任何内容。
将return添加到此:
return describeRegionsPromise.then((data) => {
https://stackoverflow.com/questions/50709302
复制相似问题