I am trying to display some data sent back from the server in the UI, but continue to hit the wall. Here is what I have going on.
I made a POST request to fetch data from an endpoint with this function
const [textData, setTextData = useState(null);
const handleSubmit = async () => {
setShowLoadingStatus(true);
loadNextStatus();
try {
// Make the POST request to the API endpoint
const response = await fetch('/api/productLaunchStatus', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
});
if (response.ok) {
const data = await response.json();
const responseData = data.text;
console.log('responseData:', responseData);
setTextData(responseData);
} else {
console.log('Response not okay:', response.statusText);
}
} catch (error) {
// Handle any unexpected errors
console.error('Error making POST request:', error.message);
}
};
The data from the formData gets sent to the server, gets processed, and here is a copy of the server response:
[
{
"Ideal Customers": {
"About Product":
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.",
},
},
{
"Ideal Customers": {
"Ideal Customer":
"ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.",
},
},
{
"Reasoning & Assumptions": {
"Reasoning":
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut.",
},
},
{
"Ideal Customer Type": {
"Customer Type": "B2B",
},
},
{
"Decision Makers": {
"Individuals": [
"Executive Directors",
"Campaign Managers",
"Fundraising Directors",
"Advocacy Directors",
"Communications Directors",
],
},
},
]
And logging textData on the frontend also returned this – An array of objects (I believe):
TEXTDATA: [
{
"Ideal Customers": {
"About Product":
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.",
},
},
{
"Ideal Customers": {
"Ideal Customer":
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.",
},
},
{
"Key Characteristics of Ideal Customers": {
"Characteristics": [
"Non-profit organizations",
"Political campaigns",
"Advocacy groups",
"Social justice organizations",
"Environmental organizations",
],
},
},
{
"Reasoning & Assumptions": {
"Reasoning":
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.",
},
},
{
"Ideal Customer Type": {
"Customer Type": "Both",
},
},
]
However, using the following snippet to display it on the frontend:
{textData && textData?.map((section, index) => (
<div key={index}>
{Object.entries(section).map(([sectionTitle, sectionContent]) => (
<div key={sectionTitle}>
<h2 className='font-bold'>{sectionTitle}</h2>
{Object.entries(sectionContent).map(([key, value]) => (
<div key={key}>
<p><strong>{key}:</strong>{value}</p>
</div>
))}
</div>
))}
</div>
))}
Returned an error:
Unhandled Runtime Error
TypeError: textData.map is not a function
Source
868 | <div>
869 | {console.log("isTextDataArray:", Array.isArray(textData))} //returned false
> 870 | {textData && textData?.map((section, index) => (
| ^
871 | <div key={index}>
872 | {Object.entries(section).map(([sectionTitle, sectionContent]) => (
873 | <div key={sectionTitle}>
{...}
The data should be an array coming back from the server, and setting it in the handleSubmit function should make textData an array (please correct me if I’m wrong).
Can you help me figure out why I can’t display this in the UI, and getting this error?