0

I have a table that stores id, user_name, state & city. When I send a request to get * from my table, I get the expected results. When I send my request based on the city, I get a 200 response, but the return only shows []. The column I'm hitting is not an integer, so I feel like const city = parseInt(request.params.city) is not correct, but I can't find anything online that calls out what should replace parseInt, if that's the issue. What am I missing?

When calling http://localhost:8080/info I get the expected results:

{
    "id": 1,
    "user_name": "Mike J",
    "state": "California",
    "city": "San Francisco"
},

{
    "id": 2,
    "user_name": "Emily R",
    "state": "Florida",
    "city": "Tampa"
},
{
    "id": 3,
    "user_name": "Tyrone S",
    "state": "New Mexico",
    "city": "Las Vegas"
},
{
    "id": 4,
    "user_name": "Eduardo G",
    "state": "Texas",
    "city": "Austin"
}

When adding the city to the call http://localhost:8080/info/Tampa I get:

[]

Here is my code:

index:

const express = require('express')

const bodyParser = require('body-parser')

const app = express()

const db = require('./queries')

const port = 8080

app.use(bodyParser.json());
app.use(express.urlencoded({extended: true}));

app.get('/', (_request, response) => {
  response.json({ info: 'Node.js, Express, and Postgres API' })
})

app.get('/info', db.getInfo)
app.get('/info/:city', db.getInfoByCity)

app.listen(port, () => {
  console.log(`App running on port ${port}.`)
})

queries:

const Pool = require('pg').Pool

const pool = new Pool({
  user: '//',
  host: '//',
  database: '//',
  password: '//',
  port: 5432,
})

const getInfo = (_request, response) => {
    pool.query('SELECT * FROM user_info ORDER BY id ASC', (error, results) => {
      if (error) {
        throw error
      }
      response.status(200).json(results.rows)
    })
  }

const getInfoByCity = (request, response) => {
    const city = parseInt(request.params.city)

    pool.query('SELECT * FROM user_info WHERE city = $1', [city], (error, results) => {
      if (error) {
        throw error
      }
      response.status(200).json(results.rows)
     })
  }

  module.exports = {
    getInfo,
    getInfoByCity,
  }

Here is my user_info table (id, user_name, state, city)

1   "Mike J"    "California"    "San Francisco"

2   "Emily R"   "Florida"   "Tampa"

3   "Tyrone S"  "New Mexico"    "Las Vegas"

4   "Eduardo G" "Texas" "Austin"

1 Answer 1

1

The issue is this:

    const city = parseInt(request.params.city)

Clearly this is a mistake. The city is a string, not an integer. Just replace with

    const city = request.params.city
Sign up to request clarification or add additional context in comments.

Comments

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.