please i am facing map is not a function error when i tried to map array of object from mongodb.
and the console.log returns the correct data but in string format. please help me on how to go about it. thanks
const [printz, setPrintz] = useState([ ])
const getOrder = async() =>{
try {
const res = await axios.get("/products/receipt/all")
setPrintz(res.data)
} catch (error) {
console.log(error);
}
}
useEffect(()=>{
getOrder()
}, [ getOrder])
console.log({DbDatas: printz}) // return the correct data
{printz && printz.map(x =>(
<tbody >
<tr>
<td>{x.product}</td>
<td>{x.price}</td>
</tr>
</tbody>
)) }
</table>
bellow is the screenshot from the mongodb
i think the problem is from the backend
router.get('/receipt/all', async (req, res) => {
try {
const getprint = await Prints.find()
res.status(200).json( getprint +'Succes!')
} catch (error) {
res.status(500).json(`Error fetching data!!! ${error}`)
}
})
Currently it's in string format. You should parse it by using JSON.parse().
In this example
JSON.parse(printz).map()