1

So I have a server side using express and a JSON file with an array of objects. but when I use fetch from the client side the data I get it as a string type. I tried to check and see if to format of the JSON is wrong but from what I could find it looks fine. I want the data as an array so I can use the .map function and put the data on the screen.

JSON file:

[
  
  {
     "title":"Death Bed",
     "artist":"Powfu",
     "artwork":"https://samplesongs.netlify.app/album-arts/death-bed.jpg",
     "url":"https://samplesongs.netlify.app/Death%20Bed.mp3",
     "id":"1"
  },
  {
     "title":"Bad Liar",
     "artist":"Imagine Dragons",
     "artwork":"https://samplesongs.netlify.app/album-arts/bad-liar.jpg",
     "url":"https://samplesongs.netlify.app/Bad%20Liar.mp3",
     "id":"2"
  },
  {
     "title":"Faded",
     "artist":"Alan Walker",
     "artwork":"https://samplesongs.netlify.app/album-arts/faded.jpg",
     "url":"https://samplesongs.netlify.app/Faded.mp3",
     "id":"3"
  },
  {
     "title":"Hate Me",
     "artist":"Ellie Goulding",
     "artwork":"https://samplesongs.netlify.app/album-arts/hate-me.jpg",
     "url":"https://samplesongs.netlify.app/Hate%20Me.mp3",
     "id":"4"
  },
  {
     "title":"Solo",
     "artist":"Clean Bandit",
     "artwork":"https://samplesongs.netlify.app/album-arts/solo.jpg",
     "url":"https://samplesongs.netlify.app/Solo.mp3",
     "id":"5"
  },
  {
     "title":"Without Me",
     "artist":"Halsey",
     "artwork":"https://samplesongs.netlify.app/album-arts/without-me.jpg",
     "url":"https://samplesongs.netlify.app/Without%20Me.mp3",
     "id":"6"
  }
]

server:

const express = require('express')
const app = express()
const port = 5000
const fs = require('fs');

const songList = fs.readFileSync('data/songs.json', 'utf8');

app.get("/songList",(req,res)=> 
{
    res.json(songList)
})



app.listen(port, () => {
    console.log(` app is listening on port ${port}`)
  })
 

client:

import { useState,useEffect } from "react" 



const MainPage = () => {
    const [songList,setSongList] = useState([{}])
    const [currentSong,setCurrentsong] = useState({})

    useEffect(()=>
    {
        fetch("/songList").then (
            response => response.json()).then ( 
            data => {setSongList(data)})
           
    },[])
     
    
    

    

    return(
        <div className="MainPageContiner">
        {
            console.log(typeof(songList))
            
        }  
        </div>
    )

}


export default MainPage

1 Answer 1

2

You need to parse the JSON using JSON.parse() to get the JS value:

fetch("/songList")
  .then(response => response.json())
  .then(data => { setSongList(JSON.parse(data)); })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.