'How to parse a json that returns an array of objects
I did some back-end changes to send 2 JSON from localhost:3001
It looks like this now:
const response = await fetch(apiURL); // fetching the data from the api
const apod = await response.json(); // parsing the data to json
const allData = [apod, lastSol()]; // creating an array with the data from the api and the last sol
res.end(JSON.stringify(allData)); // return JSON response
and my react code looks like this
import React, { useState, useEffect } from "react";
const API = "http://localhost:3001/";
export default function NasaPhoto() {
const [photoData, setPhotoData] = useState(null);
useEffect(() => {
fetchPhoto();
async function fetchPhoto() {
const res = await fetch(`${API}`);
const data = await res.json();
setPhotoData(data);
}
}, []);
if (!photoData) return <div />;
return <h3> {photoData.title} </h3>;
}
I'm having trouble parsing the new JSON when its inside an array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|