你好,我正在使用口袋妖怪API来制作一个reactjs应用程序。在我的控制器中,我有以下方法来创建一个口袋妖怪:
const createPokemon = async (name, hp, attack, defense, speed, height, weight, types, image) => {
console.log(types)
const newPokemon = await Pokemon.create({
name,
hp,
attack,
defense,
speed,
height,
weight,
image
})
types.forEach(async (t) => {
console.log(t)
const type = await Type.findOne({
where: {
name: t,
}
})
console.log(type)
await newPokemon.addType(type)
})
return newPokemon;
}
这是我的类型列表:
[
{
"name": "normal"
},
{
"name": "fighting"
},
{
"name": "flying"
},
{
"name": "poison"
},
{
"name": "ground"
},
{
"name": "rock"
},
{
"name": "bug"
},
{
"name": "ghost"
},
{
"name": "steel"
},
{
"name": "fire"
},
{
"name": "water"
},
{
"name": "grass"
},
{
"name": "electric"
},
{
"name": "psychic"
},
{
"name": "ice"
},
{
"name": "dragon"
},
{
"name": "dark"
},
{
"name": "fairy"
},
{
"name": "unknown"
},
{
"name": "shadow"
}
]
当我创建一个口袋妖怪时,我传递了口袋妖怪的所有属性,包括来自Type模型的type属性。
我在创建t
时传递它。所以我想在我的类型列表中找到t
并将其添加到我的口袋妖怪中。
这是我的请求:
{
"name": "juann",
"hp": 61,
"attack": 64,
"defense":43,
"speed": 68,
"height": 10,
"types": ["poison", "normal"]
}
console.log(types)
显示["poison", "normal"]
console.log(t)
打印poison
和normal
但是当console.log(type)
的时候我在控制台中得到null和null
1条答案
按热度按时间odopli941#
您不应该将
forEach
与await
一起使用: