Serverless + Firebase: API is not reply message - node.js

I am creating an api which will update a Firebase Realtime database, using Nodejs, Serverless, Javascript.
my .js file:
var admin = require("firebase-admin");
var serviceAccount = require("../xxxxxxx-firebase-adminsdk-xxxxxxx.json");
module.exports.saveState = (event, context, callback) => {
const body = JSON.parse(event.body);
var timestamp_create = body.timestamp_create;
try {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xxxxxxx.firebaseio.com"
});
} catch (err) {
console.log('Firebase initialization error', err.stack);
}
var db = admin.database();
var ref = db.ref('item/');
ref.child(timestamp_create).set({
state: 'empty'
}, function (error) {
if (error) {
console.log("Data could not be saved." + error);
} else {
console.log("Data saved successfully.");
return callback(null, {
headers: {
"Access-Control-Allow-Origin": "*",
},
statusCode: 200,
body: JSON.stringify({ msg: "Data saved successfully." })
});
}
});
};
I deploy API to AWS and test on Postman:
Problem and my attempts:
After saving item to Firebase Realtime Database, the api didn't reply the message. Api fell into timeout.
I checked CloudWatch, the log is printed ("Data saved successfully."), but it didn't execute the return callback at all.
I tried api in "serverless offline", it works normally, and returns the message.
The API doesn't return callback in AWS environment(lambda).
Any Suggestion is appreciated.

After long google and test, I came to a solution for this, leave it here for reference:
The firebase database connection is not released after saving data, so that AWS Lambda cannot return the callback. The simple solution is close database connection after task
var ref = db.ref('item/');
ref.child(timestamp_create).set({
state: 'empty'
}, function (error) {
if (error) {
console.log("Data could not be saved." + error);
} else {
console.log("Data saved successfully.");
db.gpOffline(); // this line will release database connection
return callback(null, {
headers: {
"Access-Control-Allow-Origin": "*",
},
statusCode: 200,
body: JSON.stringify({ msg: "Data saved successfully." })
});
}
});

Related

Fetch api not posting data to backend server

I am using cognitive service by azure which I am using Face API, In frontend the user will take picture then will call the API to check if face detected or not after that face id will be added using Add Face under FaceList as in azure documentation, after that I want to update column in database if face added successfully, here i am calling a function called senddata() which will use fetch API to send data to backend server then in server the database column will be updated, the problem is after face added successfully the senddata() function is not posting any data to backend server:
here is the code of taking picture:
const takePicture = async () => {
if (camera) {
const data = await camera.current.takePictureAsync({ quality: 0.25, base64: true });
const selfie_ab = base64ToArrayBuffer.decode(data.base64);
setTakingPic(true)
try {
const facedetect_instance_options = { ...base_instance_options };
facedetect_instance_options.headers['Content-Type'] = 'application/octet-stream';
const facedetect_instance = axios.create(facedetect_instance_options);
const facedetect_res = await facedetect_instance.post(
`/detect?returnFaceId=true&detectionModel=detection_02`,
selfie_ab
);
console.log("face detect res: ", facedetect_res.data);
if (facedetect_res.data.length) {
const add_face_instance_options = { ...base_instance_options };
add_face_instance_options.headers['Content-Type'] = 'application/octet-stream';
const add_face_instance = axios.create(add_face_instance_options);
const addface_res = await add_face_instance.post(
`/facelists/${facelist_id}/persistedFaces`, selfie_ab
);
if (addface_res.data.persistedFaceId.length) {
const status = "on hold";
const faceid = addface_res.data.persistedFaceId;
senddata(status, faceid)
console.log("Face add and send for approval: ", addface_res.data.persistedFaceId);
} else {
Alert.alert("error", "something went wrong");
}
} else {
Alert.alert("error", "Detection failure. Please make sure there is sufficient light when taking a selfie");
}
} catch (err) {
console.log("err: ", err);
}
}
};
here the senddata() function:
const senddata = (status, faceid) => {
console.log(status)
console.log(faceid)
fetch('http://*********/users/updateRegStatus', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
status: status,
faceid: faceid
})
})
.then((response) => response.json())
.then((res) => {
if (res.success === true) {
alert(res.message);
navigation.navigate('dashboard')
}
else {
alert(res.message);
}
})
}
and the following code form the backend which for updating the database:
router.post('/updateRegStatus', function (req, res, next) {
var status = req.body.status;
var faceid = req.body.faceid;
connection.query("INSERT INTO face_status (status,faceid) VALUES (?,?) ", [status, faceid], function (err, row) {
if (err) {
console.log(err);
} else {
res.send({ 'success': true, 'message': 'Your face details sent for approval' });
}
});
});
pleaser help me

Nodejs 10: Why does DynamoDB put function giving success multiple times

I am writing node js 10.x lambda function to put details into DynamoDB table.
Below is code
const AWS = require('aws-sdk');
var db = new AWS.DynamoDB.DocumentClient();
var tableName="xyz";
exports.handler = async (event) => {
// TODO implement
console.log("Event: "+ JSON.stringify(event));
var response = {
statusCode: 200,
"headers": {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
};
await db.put({
TableName: tableName,
Item: {
userid: event.userid,
}
}, (error, data) => {
if (error) {
console.log("error:"+ error);
}
else{
console.log("Success");
}
}).promise();
return response;
};
I am getting kind on random number of success return
Output execution 1
2019-11-07T07:03:45.388Z f451dfc1-01ea-41d0-a998-945cb0f18be1 INFO Success
2019-11-07T07:03:45.510Z f451dfc1-01ea-41d0-a998-945cb0f18be1 INFO Success
2019-11-07T07:03:45.511Z f451dfc1-01ea-41d0-a998-945cb0f18be1 INFO Success
Output execution 2
2019-11-07T07:08:19.270Z 3ce51f5d-bbbc-4dd6-b46f-2149ee9bb9cf INFO Success
Output execution 3
2019-11-07T07:08:27.410Z 2625bba5-b8e1-40e4-8704-7c0d486f6dff INFO Success
2019-11-07T07:08:27.431Z 2625bba5-b8e1-40e4-8704-7c0d486f6dff INFO Success
**
does anyone know the cause of this problem?
I am relatively new to node js 10.x. so please help me if I have missed something in code
**
you are using a callback and promise at the same time, remove the callback.
You can try something like
exports.handler = async (event, context) => {
const params = {
TableName: tableName,
Item: {
userid: event.userid,
}
};
try {
const data = await dynamoDB.put(params).promise();
console.log("Data: ", data);
} catch(error) {
console.error("Error:", error);
}
}

Node JS serverless rest API lambda function that first performs GET request and then POST if condition is met

I'm new to NodeJS and I'm supposed to write a serverless rest API for a online store (school project). The team I'm in is responsible of the orders customers place. To be able to place the order there has to be enough quantity in inventory (another API), so we need to check quantity in inventory using GET before we store the order in a database using POST. How should we go about this? This is what I have tried, but I end up getting timeout. The code below is based on this example: aws-node-rest-api-with-dynamodb for me to get the hang of NodeJS and serverless.
.yml file
functions:
create:
handler: todos/test.f
events:
- http:
path: todos
method: post
cors: true
test.js
const create = require("./create.js");
exports.f = function() {
const https = require('https');
https.get('url goes here', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(data);
var str = String(data);
console.log("Check: " + (str.trim() == "OK"))
create.c(); //also tried create.create();
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
create.js
'use strict';
const uuid = require('uuid');
const dynamodb = require('./dynamodb');
exports.c = function (){
console.log("Fire!");
}
module.exports.create = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
if (typeof data.text !== 'string') {
console.error('Validation Failed');
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}
const params = {
TableName: 'todos',
Item: {
id: uuid.v1(),
text: data.text,
checked: false,
createdAt: timestamp,
updatedAt: timestamp,
},
};
// write the todo to the database
dynamodb.put(params, (error) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the todo item.',
});
return;
}
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(params.Item),
};
callback(null, response);
});
};
Any thoughts on how to get this to work?

AWS Lambda NodeJS Connect to RDS Postgres Database

I'm trying to test connectivity between my Lambda and an RDS instance. I have them both on the same private subnets with all ports open in the security group. When I trigger the Lambda I do see a connection opened on the RDS instance. However, the Lambda times out after 4 minutes, 40 seconds. The PG environment variables are set in the Lambda configuration.
const { Client } = require('pg');
const client = new Client();
var hello = [
{ name: 'test', description: 'testerface' }
];
exports.handler = async (event, context, callback) => {
// Postgres Connect
client.connect();
const res = client.query('SELECT $1::text as message', ['Hello world!']);
console.log(res);
var response = {
"statusCode": 200,
"headers": {
"Content-Type" : "application/json"
},
"body": JSON.stringify(hello),
"isBase64Encoded": false
};
callback(null, response);
};
How can I get back the response from the connection in the Lambda's logs - or even better in the response body?
You need to handle the client connection better. That means catching any exceptions that the client may through and releasing the connection properly. This code will return the output of the query to the response body:
const pg = require('pg')
const pool = new pg.Pool()
async function query (q) {
const client = await pool.connect()
let res
try {
await client.query('BEGIN')
try {
res = await client.query(q)
await client.query('COMMIT')
} catch (err) {
await client.query('ROLLBACK')
throw err
}
} finally {
client.release()
}
return res
}
exports.handler = async (event, context, callback) => {
try {
const { rows } = await query("select * from pg_tables")
console.log(JSON.stringify(rows[0]))
var response = {
"statusCode": 200,
"headers": {
"Content-Type" : "application/json"
},
"body": JSON.stringify(rows),
"isBase64Encoded": false
};
callback(null, response);
} catch (err) {
console.log('Database ' + err)
callback(null, 'Database ' + err);
}
};

Access Postgres database with AWS Lambda function using Node.js

My 'handler.js' functions connects to the Postgres database and performs few queries using node.js. My function worked successfully in local, but when accessed as AWS lambda, the queries don't work.
I have also added the corresponding vpc security groups and subnet ids in 'serverless.yml'.
The error logs in 'Cloudwatch' just shows the console.log statements before and after queries and the function terminates with timed out error. I could not figure out the issue.
I have attached the example 'handler.js' code below:
var { Pool, Client } = require('pg');
export function apiTest(event, context, callback) {
var connectionString = 'postgresql://username:password#database.server.com:xxxx/dbname';
var client = new Client({
connectionString: connectionString,
})
client.connect();
console.log('Connected to PostgreSQL database');
client.query('SELECT * from table', (err, res) => {
console.log("inside query");
var jsonString = JSON.stringify(res.rows);
var jsonObj = JSON.parse(jsonString);
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
};
// Return status code 500 on error
if (err) {
const response = {
statusCode: 500,
headers: headers,
body: JSON.stringify({
status: false
})
};
callback(null, response);
client.end();
return;
}
const response = {
statusCode: 200,
headers: headers,
body: JSON.stringify(jsonObj)
};
callback(null, response);
console.log("query success")
client.end()
context.succeed(context);
})
}

Resources