0

I am trying to get information from my PostGres database to be displayed on my frontend. I'm using react, axios for http requests, node with express for my server, massive for database interactions within the server.

The current query, as I have it is returning an empty array. When running the same query in pgAdmin, it returns the right information (I will plug in a string into the query, instead of the $1) part. I suspect that the issue has to do with accepting the search name in as a parameter and applying it as $1 in the query.

Here is my http request:

handleNameSearch = () => {
        let name = this.state.searchName
        axios.get('http://localhost:3003/api/getPerson?name=' + name).then(response => {
            this.setState({
                searchedNameResults: response.data
            })
            console.log(response)
        })
    }

My endpoint in the server. Req.query.name has the correct information, so I don't think the issue is up until the query is run.

app.get('/api/getPerson', (req, res) => {
    const db = req.app.get('db');

    console.log(name)
    db.searchPeopleByName(req.query.name)
        .then(people => {
            console.log(people)
            res.status(200).send(people)
        })
        .catch(err => res.status(500).send(console.log(err)))
})

My SQL query file. I have a table named 'people' and a column named 'name'.

SELECT *
FROM people
WHERE name LIKE $1;

1 Answers1

0

Have you try with %$1% instead of $1 in the query?

Rafa Nogales
  • 614
  • 8
  • 13
  • I just tried that and got a syntax error. Internal Server Error 500 –  Jun 03 '18 at 18:19