3

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:

  • db is a postgresSQL database pool connection.

  • Trips table is already created and populated with trips having their destination value as "Nigeria".

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

  • console.log(typeof destination) // returns string.

  • console.log (destination) // returns "Nigeria"

Question

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

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. Would try that.
Specifically when constructing the URL on the client you should use something like: url = 'http://.../?destination=' + encodeURIComponent(destination)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.