wokoro douye samuel
wokoro douye samuel

Reputation: 2374

Returning posgreSQL database records with user input

I am faced with a blocker in my Node/express application.

I want to return data from a postgresSQL database table, but doing that with the query parameter passed by a user...

Assume the following:

Below is my model and controller code.

model.js

const getTrips = async ( column, value )=> {
  const query = 'SELECT * FROM trips WHERE $1 = $2';

  const { rows } = await db.query ( query, [value] );

  return rows;
}

controller.js

const getTrips = async ( req, res) => {
  const { destination } = req.query;

   const result = await Trips.getTrips( 'destination' destination );

   console.log( result.length )
}

Issue am faced with

Url = http://.../?destination="Nigeria"

When I pass the destination query parameter from the user directly , like this Trips.getTrips('destination', destination ), an empty array is returned.

However if the string equivalent of the query parameter value is passed, like this Trips.getTrips('destination', 'Nigeria'), an array with the Trips matching Nigeria as destination is returned.

Checks have done

Question

  1. Why is passing the parameter directly not returning the appropriate records and passing its string equivalent returns appropriate records?

Upvotes: 3

Views: 72

Answers (1)

Mattias Nixell
Mattias Nixell

Reputation: 438

Try taking away the quotes in your URL query (Url = http://.../?destination=Nigeria). It seems like you're querying for "Nigeria" literally (quotes included). Does this help?

Upvotes: 1

Related Questions