I wrote a simple restful API in Node.js for different HTTP-Requests which i then tried to realize with Lambda functions. My problem with the DELETE statement in Lambda is that i dont know how to pass an ID through the API gateway to delete a certain row in the mysql table.
With node.js, I just defined it through the route of the URL (ex. /contacts/:id) and then accessed it with .params.id. How would you pass an a value (the ID) for the same route (/contacts) to then use it in the handler below to delete a the row with that specific ID?
The Code i posted below works fine when u invoke it locally with --data, for example:
serverless invoke local --function delete --data "2"
the same code works too if i deploy the lambda if i pass the testdata { "id": "1" } and use event.id instead of event.
I realize that this is not the way you do it, but i ve finally run out of ideas and decided to post my issue here. :-) So, how do i pass and ID with my DELETE HTTP-Request to /contacts through the api gateway and use it in the mysql query?
module.exports.handler = (event, context, callback) =>
{
const mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ''
});
// Use the connection
connection.query('DELETE FROM sqllambdadb.contacts WHERE id=?', event, function (err, res) {
if (err) throw err;
else if (res.affectedRows>0) callback(err, 'EMail with ID: '+ event+ ' deleted!');
else callback(err, 'No row with ID: '+ event+ ' found!');
});
connection.end();
};
API Gateway maps requests to the input event object
{
"resource": "Resource path",
"path": "Path parameter",
"httpMethod": "Incoming request's method name"
"headers": {Incoming request headers}
"queryStringParameters": {query string parameters }
"pathParameters": {path parameters}
"stageVariables": {Applicable stage variables}
"requestContext": {Request context, including authorizer-returned key-value pairs}
"body": "A JSON string of the request payload."
"isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}
In your example, depending on the segement's alias your using, you'd access the id by either destructuring the appropriate event object, or referencing directly via event.pathParameters.id
const mysql = require('mysql');
module.exports.handler = (event, context, callback) => {
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ''
});
//Destructing id from event.pathParameters object
var {id} = event.pathParameters
// Use the connection
connection.query('DELETE FROM sqllambdadb.contacts WHERE id=?', id, function (err, res) {
if (err) throw err;
else if (res.affectedRows>0) callback(err, 'EMail with ID: '+ id + ' deleted!');
else callback(err, 'No row with ID: '+ id + ' found!');
});
connection.end();
};
So, i set up the yaml to add a parameter value to my URL:
delete:
handler: src/delete.handler
package:
include:
- node_modules/**
- src/delete.js
- serverlessAPI.iml
events:
- http:
path: contact-management/contacts/{id}
method: delete
cors: true
integration: LAMBDA
request:
parameters:
paths:
id: true
with this config, the id will be saved directly in the event object under event.id so you can simply access it like this:
module.exports.handler = (event, context, callback) =>{
const mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'testt123'
});
var id = event.id;
// Use the connection
connection.query('DELETE FROM sqllambdadb.Contacts WHERE id=?', id, function (err, res) {
if (err) throw err;
else if (res.affectedRows>0) callback(err, 'EMail with ID: '+ id + ' deleted!');
else callback(err, 'No row with ID: '+ id + ' found!');
});
connection.end();
};
you can test it by creating a testcase with the following input:
{
"id": "$input.params('id')"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.