When I am sending back this response from moralis as json I receive a 500 error. However, if I remove the metadata parsing then it sends back the result with all the metadata. Leaving the loop in, If I console log, userNFTs I get the response I want as json, it is just erroring on the server and not sending the json to the page.
router.get('/usernfts', async (req, res) => {
try {
const options = {
chain: 'eth',
address: '0x8a08e3Ce6CED24d376a13C544E45d4DDa02FaFEA'
}
await Moralis.start({ serverUrl, appId })
const userNFTsRes = await Moralis.Web3API.account.getNFTs(options)
let userNFTs = userNFTsRes.result
for (const item of userNFTs) {
let metadata = JSON.parse(item['metadata'])
let image = metadata['image'] ? metadata['image'] : false
if (image.startsWith('ipfs')) {
image_url = image.replace('ipfs://', '')
formated_url = `https://ipfs.io/ipfs/${image_url}`
item.image = formated_url
} else {
item.image = image
}
}
res.json(userNFTs)
} catch (err) {
res.status(500).json({ error: err })
}
})