Junjie Wang
Junjie Wang

Reputation: 1058

Node.js: How to read parameters from an axios get request?

So I have an axios request along the lines of this in my ReactJS project:

axios.get(URL, {params: {id: id}}

This would pretty much pass a get request to my backend with an URL like so:

URL?id=id

I also have a Node.js Lambda function that runs a mysql query like so:

connection.query("SELECT * FROM database.USER WHERE ID = '"+event.id+"'", function (error, results, fields))

So I know this does not work because I'm passing in a params through a URL, which does not work with event

I am trying to make my Node.js function read the id from my URL params, but I do not seem to know the syntax to do that.

Does anyone know how to get the params from a Node.js function? I've tried stuff like params.id, @id, :id, params[0].id, but none of them seem to work.

Upvotes: 0

Views: 1846

Answers (1)

Marco Talento
Marco Talento

Reputation: 2395

First of all you are exposing your backend to SQL Injection. You should NEVER concatenate your sql queries with values from the outside that you don't control. Please read about SQL injection: https://www.w3schools.com/sql/sql_injection.asp

You must use mysql2 library and prepared statements SEE: (https://github.com/sidorares/node-mysql2/blob/master/documentation/Prepared-Statements.md)

Since you are using AWS lambda you read values from the query using:

const id = event.queryStringParameters ? event.queryStringParameters.id : null;

Upvotes: 4

Related Questions