在不同路径上使用相同逻辑时表达路径问题

express path issues when using same logic on diffrent paths

id 路径有效(我从 json 得到结果),但即使 pokeName 使用相同的逻辑,我也没有从中得到任何数据。我们错过了什么?

JSON: https://github.com/Biuni/PokemonGO-Pokedex/blob/master/pokedex.json

const Pokemons = mongoose.model('Pokemons', {
  id: Number,
  name: String,
  type: Array,
})
_____

app.get('/pokemons', async (req, res) => {
  try {
    if (!allPokemons) {
      res.status(404).send('No data to show')
    } else {
      res.json(allPokemons.pokemon) /*[1].name*/
    }
  } catch (error) {
    res.status(400).json({ error: 'Not found' })
  }
})

_____

app.get('/pokemons/id/:id', async (req, res) => {
  const { id } = req.params
  const pokemon = allPokemons.pokemon.find((item) => item.id === +id)
  try {
    if (!pokemon) {
      res.status(404).send('No pokemon found with this ID')
    } else {
      res.json(pokemon)
    }
  } catch (error) {
    res.status(400).json({ error: 'Not found' })
  }
})

_____ 

app.get('/pokemons/name/:pokeName', async (req, res) => {
  const { pokeName } = req.params
  const pokemon = allPokemons.pokemon.find((item) => item.name === +pokeName)
  try {
    if (!pokemon) {
      res.status(404).send('No pokemon found with this name')
    } else {
      res.json(pokemon)
    }
  } catch (error) {
    res.status(400).json({ error: 'Not found' })
  }
})

您正在尝试将名称转换为数字(可能是因为 id 一个数字,而您复制了它的代码):

item.name === +pokeName

但事实并非如此,因此这将导致 NaN。删除 +:

item.name === pokeName