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' })
}
})
uj5u.com熱心網友回復:
您正在嘗試將名稱轉換為數字(可能是因為id 是數字,并且您復制了它的代碼):
item.name === pokeName
但事實并非如此,所以這將導致NaN. 洗掉 :
item.name === pokeName
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485260.html
標籤:表示
