简体   繁体   English

express.js / postgresql 获取请求返回[]

[英]express.js / postgresql Get request returns []

I have a table that stores id, user_name, state & city.我有一个存储 id、user_name、state 和城市的表。 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 [].当我根据城市发送请求时,我收到 200 响应,但返回仅显示 []。 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.我要访问的列不是 integer,所以我觉得 const city = parseInt (request.params.city) 不正确,但我在网上找不到任何可以说明应该替换parseInt的内容,如果这是问题所在. What am I missing?我错过了什么?

When calling http://localhost:8080/info I get the expected results:调用 http://localhost:8080/info 时,我得到了预期的结果:

{
    "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:将城市添加到呼叫 http://localhost:8080/info/Tampa 时,我得到:

[]

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)这是我的 user_info 表(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"

The issue is this:问题是这样的:

    const city = parseInt(request.params.city)

Clearly this is a mistake.显然这是一个错误。 The city is a string, not an integer.城市是一个字符串,而不是 integer。 Just replace with只需替换为

    const city = request.params.city

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM