Reputation: 557
I have a function in node.js which needs to be hosted as a lambda in AWS. But before we host it, we need to check if the function works properly. I have been trying to run the function using lambda-local. But the challenge is, I have external HTTP calls in my logic. I'm using Axios for that, but I get response as undefined. Is there any way to hit those external HTTP end-points and get response? Below is the command line log. Thanks in advance.
D:\Billing_&_Consumption\P2D-Phase2 - Billings\POC\scripts\controller>lambda-local -l DocumentsHandler.js -h getDocumentsList -e event.js
warning Using both auth systems: aws_access_key/id and secret_access_token !
info: START RequestId: 155ee184-1a7b-464a-eeac-d5593bde5abe
Response: undefined
TypeError: Cannot read property 'access_token' of undefined
at Object.DocumentsController.getDocumentsList (D:\Billing_&_Consumption\P2D-Phase2 - Billings\POC\scripts\controller\DocumentsController.js:26:87)
at Object._executeSync (C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\lib\lambdalocal.js:169:47)
at Object._execute [as execute] (C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\lib\lambdalocal.js:40:22)
at C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\bin\lambda-local:133:21
at Object.<anonymous> (C:\Users\429732\AppData\Roaming\npm\node_modules\lambda-local\bin\lambda-local:169:3)
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)
error: End - Error
error: ------
error: {
"errorMessage": "Cannot read property 'access_token' of undefined",
"errorType": "TypeError",
"stackTrace": [
"Object.DocumentsController.getDocumentsList (D:\\Billing_&_Consumption\\P2D-Phase2 - Billings\\POC\\scripts\\controller\\DocumentsController.js:26:87)",
"Object._executeSync (C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\lib\\lambdalocal.js:169:47)",
"Object._execute [as execute] (C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\lib\\lambdalocal.js:40:22)",
"C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\bin\\lambda-local:133:21",
"Object.<anonymous> (C:\\Users\\429732\\AppData\\Roaming\\npm\\node_modules\\lambda-local\\bin\\lambda-local:169:3)",
"Module._compile (module.js:652:30)",
"Object.Module._extensions..js (module.js:663:10)",
"Module.load (module.js:565:32)",
"tryModuleLoad (module.js:505:12)",
"Function.Module._load (module.js:497:3)"
]
}
error: ------
error: Lambda failed in 135ms.
I can provide you with the axios call and its resolving and rejection
public getAccessToken(): AxiosPromise<any> {
return axios({
method: 'post',
url: `${URL}`,
data: this.data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}
Service.ts
this.documentStoreBroker.getAccessToken()
.then((token: any) => {
console.log(token);
return new ADSAccessToken(token);
})
.catch(error => {
console.log(error);
throw new InternalServerError('99x100');
});
}
Upvotes: 0
Views: 427
Reputation: 179
First you should try to hit the endpoint for the token via Postman. You can verify if your settings for the endpoint are correct and then use the same for axios.
If that works you can continue in your lambda and use Nock to mock the response you are expecting (like the response you got in postman). Nock intercepts the http calls so that you won't hit the real endpoint any more and you can concentrate on the rest of the logic.
However the 'nocked' code should not make it into production because it prevents hitting the real endpoint so I'd suggest to use some kind of testing environment to write tests which do not affect the production code.
Upvotes: 1