[英]AWS Lambda/API Gateway - Pass an ID to delete a row in mysql with node.js
I wrote a simple restful API in Node.js for different HTTP-Requests which i then tried to realize with Lambda functions.我在 Node.js 中为不同的 HTTP 请求编写了一个简单的 Restful API,然后我尝试使用 Lambda 函数来实现。 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.
我在 Lambda 中使用 DELETE 语句的问题是我不知道如何通过 API 网关传递 ID 以删除 mysql 表中的某一行。
With node.js, I just defined it through the route of the URL (ex. /contacts/:id) and then accessed it with .params.id.使用 node.js,我只是通过 URL 的路由(例如 /contacts/:id)定义它,然后使用 .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?
您将如何为同一路由 (/contacts) 传递一个值(ID),然后在下面的处理程序中使用它来删除具有该特定 ID 的行?
The Code i posted below works fine when u invoke it locally with --data, for example:当您使用 --data 在本地调用它时,我在下面发布的代码可以正常工作,例如:
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.如果我通过 testdata { "id": "1" } 并使用 event.id 而不是 event,那么如果我部署 lambda,同样的代码也可以工作。
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?
:-) 那么,我如何通过 api 网关将带有 DELETE HTTP 请求的 ID 传递到 /contacts 并在 mysql 查询中使用它?
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 API Gateway 将请求映射到输入事件对象
{
"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
在您的示例中,根据您使用的段别名,您可以通过解构适当的事件对象或直接通过
event.pathParameters.id
引用来访问 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:因此,我设置了 yaml 以将参数值添加到我的 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:使用此配置,id 将直接保存在 event.id 下的事件对象中,因此您可以像这样简单地访问它:
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')"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.