【发布时间】:2019-06-11 00:45:22
【问题描述】:
我通过命令行将大约 9 个参数传递给 Node JS 脚本。 这是我的命令:
node awsInvokeDelete.js DELETE https://Test1234.execute-api.us-west-2.amazonaws.com us-west-2 /qa/transit-connectivity/api/v1/sites/tdcloudtsttd03 AKIAJ4Y5DGqwewqeqw CFdAgsdtqweqwe/SKqDezdqweewofWrUXXBbQoMy '{\"change_request\":\"chg0123456\"}'
我在命令行参数中将查询参数作为 JSON 传递,该参数是节点 JS 脚本中的 process.argv[9]。如果我将值传递给所有参数,但在某些情况下 process.argv[8] 将为空,它会完美运行。当我在 process.argv[8] 中传递空值时,它实际上将 argv[9] 作为 argv[8]。
如何在命令行中为下面的脚本传递空参数值。
var apigClientFactory = require('aws-api-gateway-client').default;
let awsMethod = process.argv[2],
awsEndpoint = process.argv[3],
awsRegion = process.argv[4],
awsPathTemplate = process.argv[5],
awsAccessKey = process.argv[6],
awsSecreteKey = process.argv[7],
awsPathParams = process.argv[8],
awsAdditionalParams = JSON.parse(process.argv[9] || '{}');
var apigClient = apigClientFactory.newClient({
invokeUrl: awsEndpoint, // REQUIRED
accessKey: awsAccessKey, // REQUIRED
secretKey: awsSecreteKey, // REQUIRED
region: awsRegion, // REQUIRED: The region where the API is deployed.
retryCondition: (err) => { // OPTIONAL: Callback to further control if
request should be retried. Uses axon-retry plugin.
return err.response && err.response.status === 500;
}
});
var param = awsPathParams;
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = awsPathTemplate;
var method = awsMethod;
var additionalParams = { queryParams: awsAdditionalParams, };
console.log(additionalParams);
var body = {};
apigClient.invokeApi(param, pathTemplate, method, additionalParams, body)
.then(function(result) {
//console.log(result.data + ": " +result)
console.log(result.response.data)
}).catch(function(result) {
console.log(result.response.data)
});
这是输出:args[8] 的值应该显示为 args[9]
args[8]: {"change_request":"chg0123456"}
args[9]: [object Object]
【问题讨论】:
标签: node.js amazon-web-services command-line-arguments command-prompt