AWS Lambda NodeJs unable to return response - javascript

I am trying to implement a simple Node Js example on AWS Lambda,
The code has a sample example of Async library.
The code works fine, but from some reason the Lambda Function return null response.
I am new to Node as well, please help.
Following is the code -
var async = require('async');
exports.handler = async (event, context, callback) => {
async.waterfall([
function(callback) {
console.log("ONE");
callback(null, 1);
},
function(resp, callback) {
console.log("TWO : ", resp);
callback(null, 2);
},
function(resp, callback){
console.log("THREE : ", resp);
callback(null, "Done");
}
],
function(err, resp) {
let response = {};
if (err) {
console.log("Error",err);
response = {
statusCode: 500,
body: JSON.stringify('Error!'),
};
return response;
} else {
console.log("Success",resp);
response = {
statusCode: 200,
body: JSON.stringify('Ok!'),
};
return response;
}
});
};
Following are the CloudWatch logs -
START RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861 Version: $LATEST
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 ONE
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 TWO : 1
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 THREE : 2
2019-03-21T15:15:26.597Z ab9aa426-dfc9-44ac-8d96-a4f102e30861 Success Done
END RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861
REPORT RequestId: ab9aa426-dfc9-44ac-8d96-a4f102e30861 Duration: 37.28 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 67 MB
I used the sample Node blueprint, which works fine -
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};

Since you're already using Node 8, you don't need to use the outdated, confusing callback approach anymore. Use await instead
exports.handler = async (event) => {
try {
await somePromise1
await somePromise2
await somePromise3
console.log("Success", resp);
response = {
statusCode: 200,
body: JSON.stringify('Ok!'),
};
return response;
} catch (err) {
console.log("Error", err);
response = {
statusCode: 500,
body: JSON.stringify(err),
};
return response;
}
};
where somePromise1, somePromise2 and somePromise3 are your promisified callbacks.
More on async/await here.

Try to remove the async from the handler and use callback instead of return:
var async = require('async');
exports.handler = (event, context, callback) => {
async.waterfall([
function(callback) {
console.log("ONE");
callback(null, 1);
},
function(resp, callback) {
console.log("TWO : ", resp);
callback(null, 2);
},
function(resp, callback){
console.log("THREE : ", resp);
callback(null, "Done");
}
],
function(err, resp) {
let response = {};
if (err) {
console.log("Error",err);
callback(null, {
statusCode: 500,
body: 'Error!',
});
} else {
console.log("Success",resp);
callback(null, {
statusCode: 200,
body: 'Ok!',
});
}
});
};

Related

defining a await for the function in Node.js

I am trying to get the response data from 3 APIs which is depended on each other. when I call each external API the response has an array of external APIs, as I need all the 3 external APIs responses.
Here is the code:
const options = {
JSON: true,
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
"Authorization": process.env.authID,
},
body: {},
uri: ''
};
app.post('/sync', async (req, res) => {
try {
let getFirstAPI = Object.assign({}, options);
getFirstAPI.uri = 'https://abcde'; // first API
request(getFirstAPI, function (err, httpResponse, body) {
Promise.all(body.projects.map((prct) => getVuln(prct))).then(rs => {
res.JSON({ message: rs });
});
});
}
});
async function getVuln(prodId) {
try {
let getSecondAPI= Object.assign({}, options);
getSecondAPI.uri = 'abcd' + prodId.id + '/abc'; //Second API
return await new Promise((resolve, reject) => {
request(getSecondAPI, function (err, httpResponse, body) {
let final = {
products: [],
dataList: []
}
final.products.push({
product_id: prodId.id,
product_name: prodId.abc,
product_ver: prodId.wdc
})
body.issues.forEach(csa => {
final.dataList.push({
product_id: prodId.id,
version: csa.Versions,
name: csa.Name,
pathJsonValue:await getPathfun(csa.link) //Here i am getting error "SyntaxError: Unexpected identifier"
});
});
resolve(final);
});
})
}
}
function getPathfun(urlStr) {
let getPathUrl = Object.assign({}, options);
getPathUrl.url = urlStr;
return new Promise((resolve, reject) => {
request(getPathUrl, function (err, httpResponse, body) {
resolve(body);
});
});
}
at the "pathJsonValue" I am getting an error while running the code, if I remove the await then "pathJsonValue" is empty. I have attached the error image below. please help me with this issue.
request(getSecondAPI, function (err, httpResponse, body) {
Callback must be an ASYNC function to have the possibility to use 'await' keyword inside it.
'await' keyword doesnt work in "forEach" method. Use for...of loop instead.

callback is not a function - castv2

I'm following this http://siglerdev.us/blog/2021/02/26/google-home-message-broadcast-system-node-js/31 which uses this library castv2-client to send messages to my google home. It works. I get the messages no problem, but the code throws
C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72
callback(null, response.status.volume);
^
TypeError: callback is not a function
at C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72:5 ver.js:72
at fn.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\request-response.js:27:7)
at fn.emit (events.js:203:15)
at Channel.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\controller.js:16:10) s\receiver.js:72:5
at Channel.emit (events.js:198:13) lib\controllers\request-response.js:27:7)
at Client.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2\lib\channel.js:23:10) ient\lib\controllers\controller.js:16:10)
at Client.emit (events.js:203:15)
at PacketStreamWrapper.onpacket (C:\Users\Phil\Documents\google home\node_module\channel.js:23:10)s\castv2\lib\client.js:81:10)
at PacketStreamWrapper.emit (events.js:198:13) s\castv2\lib\client.js:81:10)
at TLSSocket.<anonymous> (C:\Users\Phil\Documents\google home\node_modules\castv2\lib\packet-stream-wrapper.js:28:16)
What's wrong with the code that is throwing this AND/OR how can I fix it so it's either more graceful in catching error and doesn't throw since the message still delivers to google home or fix it to not throw this at all?
I appreciate any help!
I believe it's here in the castv2-client library that it's referencing, but I haven't been able to make it happy.
ReceiverController.prototype.launch = function(appId, callback) {
this.request({ type: 'LAUNCH', appId: appId }, function(err, response) {
if(err) return callback(err);
if(response.type === 'LAUNCH_ERROR') {
return callback(new Error('Launch failed. Reason: ' + response.reason));
}
callback(null, response.status.applications || []);
});
};
my code
var Client = require('castv2-client').Client;
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
const googleTTS = require('google-tts-api');
var App = {
playin: false,
DeviceIp: "",
Player: null,
GoogleHome: function (host, url) {
var client = new Client();
client.connect(host, function () {
client.launch(DefaultMediaReceiver, function (err, player) {
client.setVolume({ level: 1 });
var media = {
contentId: url,
contentType: 'audio/mp3',
streamType: 'BUFFERED'
};
App.Player = player;
App.Player.load(media, { autoplay: true }, function (err, status) {
App.Player.on('status', function (status) {
if (status.playerState === "IDLE" && App.playin === false) {
client.close();
}
});
});
});
});
client.on('error', function (err) {
console.log('Error: %s', err.message);
client.close();
});
},
run: function (ip, text) {
App.DeviceIp = ip;
const url = googleTTS.getAudioUrl(text, {
lang: 'en-US',
slow: false,
host: 'https://translate.google.com',
});
App.GoogleHome(App.DeviceIp, url, function (res) {
console.log(res);
})
},
broadcast: function(text){
const ips = '192.168.0.15'.split(","); //From config, 192.168.68.105,192.168.68.107,192.168.68.124
for (var s of ips){
App.run(s, text);
}
}
}
App.broadcast("Broadcasted to all of the devices"); //Only works if you did step 4.5
The error you reported:
C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72
callback(null, response.status.volume);
^
TypeError: callback is not a function
at C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72:5
seems to be related to the invocation of the method setVolume in your client:
client.setVolume({ level: 1 });
Please, consider review the source code of receiver.js in the castv2-client library:
ReceiverController.prototype.setVolume = function(options, callback) {
var data = {
type: 'SET_VOLUME',
volume: options // either `{ level: 0.5 }` or `{ muted: true }`
};
this.request(data, function(err, response) {
if(err) return callback(err);
callback(null, response.status.volume);
});
};
The library is claiming because you aren't providing a proper callback when invoking that function.
I have never used the library but probably providing something similar to the following callback could be of help:
client.setVolume({ level: 1 }, function(err, volume) {
if (err) {
// Handle error as appropriate
console.log('Error on setVolume:', err);
} else {
console.log('Volume:', volume)
}
});
Your final code would look like this:
var Client = require('castv2-client').Client;
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
const googleTTS = require('google-tts-api');
var App = {
playin: false,
DeviceIp: "",
Player: null,
GoogleHome: function (host, url) {
var client = new Client();
client.connect(host, function () {
client.launch(DefaultMediaReceiver, function (err, player) {
client.setVolume({ level: 1 }, function(err, volume) {
if (err) {
// Handle error as appropriate
console.log('Error on setVolume:', err);
} else {
console.log('Volume:', volume)
}
});
var media = {
contentId: url,
contentType: 'audio/mp3',
streamType: 'BUFFERED'
};
App.Player = player;
App.Player.load(media, { autoplay: true }, function (err, status) {
App.Player.on('status', function (status) {
if (status.playerState === "IDLE" && App.playin === false) {
client.close();
}
});
});
});
});
client.on('error', function (err) {
console.log('Error: %s', err.message);
client.close();
});
},
run: function (ip, text) {
App.DeviceIp = ip;
const url = googleTTS.getAudioUrl(text, {
lang: 'en-US',
slow: false,
host: 'https://translate.google.com',
});
App.GoogleHome(App.DeviceIp, url, function (res) {
console.log(res);
})
},
broadcast: function(text){
const ips = '192.168.0.15'.split(","); //From config, 192.168.68.105,192.168.68.107,192.168.68.124
for (var s of ips){
App.run(s, text);
}
}
}
App.broadcast("Broadcasted to all of the devices"); //Only works if you did step 4.5

test is exiting before the method which has async.waterfall, is executed

I'm currently writing unit tests for an API. One of the helper functions is the async.waterfall to avoid "callback hell". I made this method async and await where the method is called.
The test is exiting saying Number of calls: 0 in Node.js. I'm using Jest as my test framework.
Method being tested:
outletInfoController.fetchAllOutletsByUserIdAndCityId = async function (req, res) {
var userId = req.body.userId;
var cityId = req.body.cityId;
await outletInfoHelper.fetchAllOutletsByUserIdAndCityId(userId, cityId, function (error, result) {
if (error || !result) {
responseUtils.buildAndRespond(commonConst.CODE.FAILURE_RESPONSE_CODE, commonConst.MESSAGE.FAILURE_RESPONSE_MESSAGE,
error, null, res);
} else {
responseUtils.buildAndRespond(commonConst.CODE.SUCCESS_RESPONSE_CODE, commonConst.MESSAGE.SUCCESS_RESPONSE_MESSAGE,
null, result, res);
}
});
};
Method which is using async.waterfall:
outletInfoHelper.fetchAllOutletsByUserIdAndCityId = async function (userId, cityId, outerCallback) {
async.waterfall(
[function getRestaurantsByUserId(callback) {
//do something
}, function getRestaurantsDetails(restIds, callback) {
//do something
}, function getRestaurantsUserCount(restaurants, callback) {
//do something
}], function (error, results) {
//do something
});
}
Test code snippet:
test("outletInfo_success_response", async () => {
let req = {
body: {
userId: 8881,
cityId: 211
}
};
let restIds = [];
await factory.restaurantListFactory((result) => {
restIds = result;
});
await fetchUserRestaurantObjFromUserId.mockImplementation((userId, cb) => {
cb(null, restIds);
});
let mockResponse = [];
await factory.restaurantObjectsFactory(function (result) {
mockResponse = result;
});
let successResp = {
statusMessage: 'success',
data: mockResponse,
errorMessage: null,
statusCode: 0
}
await fetchRestaurantsFromIdsAndCityId.mockImplementation((restIds, cityId, cb) => {
cb(null, mockResponse);
});
let response;
await factory.getUserRestMap((result) => {
response = result;
});
await fetchUsersFromOutletId.mockImplementation((restIds, cb) => {
cb(null, response);
});
let res = await getResponseObj();
await outletInfoController.fetchAllOutletsByUserIdAndCityId(req, res);
expect(res.send).toBeCalledWith(successResp);
});

AWS Lambda call 3 async functions by 1 Lambda call

Ok, i'm done. Please someone help me :(
I don't know how js and lambda works
What i have to do:
Send GET request and get response.
Write data from response to DynamoDb
I can do it 1by1 but can't do everything by 1 lambda call.
My code:
const https = require('https');
const crypto = require("crypto");
const AWS = require('aws-sdk');
const DynamoDb = new AWS.DynamoDB({region: 'eu-central-1'});
exports.handler = async (event) => {
let response;
console.log("Start");
let steamTicket;
let steamId;
if(event.body){
const body = JSON.parse(event.body);
if(body.steamticket && body.steamid){
steamTicket = body.steamticket;
steamId = body.steamid;
}
else{
response = {
statusCode: 400,
body: JSON.stringify({
authenticated: false,
reason: 'cant find steamid or steamticket in your request'
})
};
return response;
}
}
else{
response = {
statusCode: 400,
body: JSON.stringify({
authenticated: false,
reason: 'cant find request body'
})
};
return response;
}
await httprequest(steamTicket).then((data) =>{
if(data.response && data.response.params){
if(data.response.params.result == 'OK' && data.response.params.steamid == steamId){
console.log(JSON.stringify(data));
const sessionId = crypto.randomBytes(16).toString("hex");
console.log('Generated session id: ' + sessionId);
PutToDB(sessionId, steamId);
}
else{
response = {
statusCode: 400,
body: JSON.stringify({
authenticated: false,
reason: 'steam response is not OK or session != steamId'
})
};
return response;
}
}
else{
response = {
statusCode: 400,
body: JSON.stringify({
authenticated: false,
reason: 'invalid response from steam: ' + JSON.stringify(data)
})
};
return response;
}
});
};
async function PutToDB(sessionId, steamId){
var WriteParams = {
RequestItems:{
SteamSessions: []
}
};
WriteParams.RequestItems.SteamSessions.push({
PutRequest:{
Item: {
SteamId: {S: steamId},
SessionId: {S: sessionId},
ttl: {N: (Math.floor(Date.now() / 1000) + 600).toString()}
}
}
});
console.log('SessionIdToWrite: ' + sessionId);
return new Promise((resolve, reject) =>{
DynamoDb.batchWriteItem(WriteParams, function(err, data){
if(err){
console.log("Error", err);
}
else{
console.log("Success write", JSON.stringify(data));
}
})
})
}
async function httprequest(steamTicket) {
return new Promise((resolve, reject) => {
const options = {
host: 'partner.steam-api.com',
path: '/ISteamUserAuth/AuthenticateUserTicket/v1/?key=somekey&appid=someid&ticket=' + steamTicket,
port: 443,
method: 'GET'
};
const req = https.request(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
});
}
I lost way already, i'm not even sure it should work like that.
And most confusing thing! This b give me this test results:
Run 1:
2021-03-05T13:28:47.741Z INFO Start
2021-03-05T13:28:48.612Z INFO {"response":{"params":{"result":"OK","steamid":"mysteamid","ownersteamid":"mysteamid","vacbanned":false,"publisherbanned":false}}}
2021-03-05T13:28:48.650Z INFO Generated session id: 6a5633a5f862d8663d0fe546a9c89feb
2021-03-05T13:28:48.650Z INFO SessionIdToWrite: 6a5633a5f862d8663d0fe546a9c89feb
DynamoDb is empty, here we can't see log from DynamoDb.batchWriteItem result.
Run 2:
2021-03-05T13:29:53.308Z INFO Start
2021-03-05T13:29:53.674Z INFO Success write {"UnprocessedItems":{}}
2021-03-05T13:29:54.048Z INFO {"response":{"params":{"result":"OK","steamid":"mysteamid","ownersteamid":"mysteamid","vacbanned":false,"publisherbanned":false}}}
2021-03-05T13:29:54.048Z INFO Generated session id: 05c62de782202fc100cea9d47e38242c
2021-03-05T13:29:54.048Z INFO SessionIdToWrite: 05c62de782202fc100cea9d47e38242c
And after second run i can see in DynamoDb sessionId from FIRST RUN (6a5633a5f862d8663d0fe546a9c89feb)
If i run it again, there will be id from 2nd run
I think it continues to run previous tasks on new run? Or what? I'm lost
Thank you for any help with it
You need to call reject / resolve in the DynamoDb.batchWriteItem call.
return new Promise((resolve, reject) =>{
DynamoDb.batchWriteItem(WriteParams, function(err, data){
if(err){
console.log("Error", err);
reject(err);
}
else{
console.log("Success write", JSON.stringify(data));
resolve();
}
})
})

How do I use async-await properly in the following situation?

I'm struggling with async await block. I have looked the multiple resources on the net but I just can't understand what I'm doing wrong here :
app.post('/api/my-api', async (req, res, next) => {
try {
filecontent = req.files.userFile.data.toString('utf8')
console.log("decoded file : ", filecontent);
let encoded_file = new Buffer(filecontent).toString('base64');
var apiClass = new RPC("http://localhost:1006", "my-api");
//the asynchronous function :
const answ = await apiMethod.call("api", [{"file" : encoded_file, "fileName":req.files.userFile.name}], res.json);
//the code I'd like to execute only after the previous function has finished :
console.log("answer : ", answ);
console.log("answering...");
res.json(answ);
} catch (err) {
console.log(err);
}
Apparently my console.log are executed before the the await line is done. I can tell because there's a console.log() in the asynchronous function too, and my res.json is sent before I receive answ.
How do I make sure the asynchronous function finishes before the rest of the code ?
Edit : here is the apiMethod.call function :
call(id, params) {
let options = {
url: this.url,
method: "post",
headers:
{
"content-type": "text/plain"
},
body: JSON.stringify( {"jsonrpc": "2.0", "id": id, "method": this.procedure, "params": params })
};
console.log(options);
request(options, (error, response, body) => {
if (error) {
console.error('An error has occurred: ', error);
} else {
console.log('Post successful: response: ', body);
}
});
}
Issue is in call function. Since it has async code (request call), it should be wrapped in promise which should resolve from callback function of request.
Updating call function to something like below should help:
function call(id, params) {
return new Promise((resolve, reject) => {
let options = {
url: this.url,
method: "post",
headers: {
"content-type": "text/plain",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: id,
method: this.procedure,
params: params,
}),
};
console.log(options);
request(options, (error, response, body) => {
if (error) {
console.error("An error has occurred: ", error);
reject(error);
} else {
console.log("Post successful: response: ", body);
resolve(body);
}
});
})
}

Categories