简体   繁体   English

获取响应 db.json 是一个 object 而不是一个对象数组

[英]Fetch response db.json is one object instead an array of objects

I am trying to display on a single page react app, the icons from items, but as a response from the fetch, instead of getting an array of objects, I'm getting only one object, which doesnt have the data from my db.json.我试图在单页反应应用程序上显示来自项目的图标,但作为来自获取的响应,而不是获取对象数组,我只得到一个 object,它没有来自我的数据库的数据。 json。

My question is: How can I reach the "displayIcon" if I can't get a correct response?我的问题是:如果我无法得到正确的回复,我该如何到达“displayIcon”?

Here is my function, and below the db.json:这是我的 function,在 db.json 下面:

 const [weapons, setWeapons] = useState(null); useEffect(() => { console.log('effect triggered'); fetch('http://localhost:3001/data/', { headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }).then((res) => res.json()).then((data) => setWeapons(data)); }, []); console.log('render', weapons)

 "data": [ { "uuid": "1", "displayName": "Odin", "category": "EEquippableCategory::Heavy", "defaultSkinUuid": "f454efd1-49cb-372f-7096-d394df615308", "displayIcon": "https://media.valorant-api.com/weapons/63e6c2b6-4a8e-869c-3d4c-e38355226584/displayicon.png", "killStreamIcon": "https://media.valorant-api.com/weapons/63e6c2b6-4a8e-869c-3d4c-e38355226584/killstreamicon.png", "assetPath": "ShooterGame/Content/Equippables/Guns/HvyMachineGuns/HMG/HMGPrimaryAsset", }, { "uuid": "2", "displayName": "Ares", "category": "EEquippableCategory::Heavy", "defaultSkinUuid": "5305d9c4-4f46-fbf4-9e9a-dea772c263b5", "displayIcon": "https://media.valorant-api.com/weapons/55d8a0f4-4274-ca67-fe2c-06ab45efdf58/displayicon.png", "killStreamIcon": "https://media.valorant-api.com/weapons/55d8a0f4-4274-ca67-fe2c-06ab45efdf58/killstreamicon.png", "assetPath": "ShooterGame/Content/Equippables/Guns/HvyMachineGuns/LMG/LMGPrimaryAsset", }

If you are trying to render a single image, you can get the item by uuid and render the image (or anything else).如果您尝试渲染单个图像,则可以通过 uuid 获取项目并渲染图像(或其他任何内容)。

Here's an example component That accesses the weapon with uuid of 2. In your app you'd probably want to pass uuid to this component using props, but the same logic will apply.这是一个使用 uuid 为 2 访问武器的示例组件。在您的应用程序中,您可能希望使用道具将 uuid 传递给该组件,但同样的逻辑将适用。 strong text强文本

 // get weapon by uuid const ShowIcon = () => { let weapons = { "data": [ { "uuid": "1", "displayName": "Odin", "category": "EEquippableCategory::Heavy", "defaultSkinUuid": "f454efd1-49cb-372f-7096-d394df615308", "displayIcon": "https://media.valorant-api.com/weapons/63e6c2b6-4a8e-869c-3d4c-e38355226584/displayicon.png", "killStreamIcon": "https://media.valorant-api.com/weapons/63e6c2b6-4a8e-869c-3d4c-e38355226584/killstreamicon.png", "assetPath": "ShooterGame/Content/Equippables/Guns/HvyMachineGuns/HMG/HMGPrimaryAsset", }, { "uuid": "2", "displayName": "Ares", "category": "EEquippableCategory::Heavy", "defaultSkinUuid": "5305d9c4-4f46-fbf4-9e9a-dea772c263b5", "displayIcon": "https://media.valorant-api.com/weapons/55d8a0f4-4274-ca67-fe2c-06ab45efdf58/displayicon.png", "killStreamIcon": "https://media.valorant-api.com/weapons/55d8a0f4-4274-ca67-fe2c-06ab45efdf58/killstreamicon.png", "assetPath": "ShooterGame/Content/Equippables/Guns/HvyMachineGuns/LMG/LMGPrimaryAsset", } ] } let weapon = weapons.data.filter(i => i.uuid == 2? 1: 0); return( <div> <img src={weapon[0].displayIcon} /> </div> ); } ReactDOM.render(<ShowIcon />, document.getElementById('icon'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="icon"></div>

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

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