This Question is an additinal question atached to axios resoponse work when not saving response in state and when setting state fails.
I didnt get a true answer to my question and figured I should give some examples.
I don't understand why when I'm printing my response I see the result but when I try to save it in a state I'm getting an empty array (in the response itself which mean my new state will be an empty array to...)
I've researched through the internet and all I found was examples where people didn't use promise as they should and because of that couldn't save the response but could print it.
my code
useEffect(()=>{
props.getAllOpen.then(res=>{
let check = res.data.data;
console.log(check); or [console.log(res.data.data)]
});
},[]);
Result in console
Array(2)
0: {Stations: Array(2), _id: "60c5be4be3f2262294dff893", Type: "d"}
1: {Stations: Array(2), _id: "60c5be4be3f2262294dff892", Type: "d"}
My problem
const [Data,setData] = useState([]);
useEffect(()=>{
props.getAllOpen.then(res=>{
setData(res.data.data);
console.log(res.data.data);
});
},[]);
Result in console
[]
What Am I doing wrong? any help will be appreciated.
Thank You for taking the time to help me :)
--->Full Component<---
import {React,useEffect,useState} from 'react';
let dateFormat = require('dateformat');
const NotificationTable = (props)=>{
const [Data,setData] = useState([]);
useEffect(()=>{
props.getAllOpen.then(res=>{
setData(res.data.data);
});
},[]);
// get date return true if the date is the date of current day
function isSameDay(date) {
let today = new Date();
if(today.getMonth()===date.getMonth() && today.getFullYear()===date.getFullYear()
&& today.getDay()=== date.getDay())
return true;
return false;
}
return(
<div className = "Notification-Table">
<div className = "Row"> <div className = "Header-Cell">end date</div><div className = "Header-Cell">start date</div> <div className = "Header-Cell">duplicate</div> <div className = "Header-Cell">סוג</div> <div className = "Header-Cell">s</div></div>
{ [...Data.filter(e=>e.Close==="1970-01-01T00:00:00.000Z").splice(0,21),...Array(22-Data.filter(e=>e.Close==="1970-01-01T00:00:00.000Z").splice(0,21).length)].map((d,index)=>{
if(d)
{
return(<div key = {d._id} className = "Row">
<div className = "Cell">{new Date(d.Close).getFullYear()===1970?"Live":isSameDay(new Date(d.Close))?dateFormat(new Date(d.Close),"HH:MM:ss"):dateFormat(new Date(d.Close),"dd-mm-yyyy")}</div>
<div className = "Cell">{isSameDay(new Date(d.Open))?dateFormat(new Date(d.Open),"HH:MM:ss"):dateFormat(new Date(d.Close),"dd-mm-yyyy")}</div>
<div className = "Cell">{d.Duplicate}</div>
<div className = "Cell">{d.Type}</div>
<div className = "Cell">{d.Stations}</div>
</div>);}
else
{
return(<div key = {index} className = "Row">
<div className = "Empty-Cell"></div>
<div className = "Empty-Cell"></div>
<div className = "Empty-Cell"></div>
<div className = "Empty-Cell"></div>
<div className = "Empty-Cell"></div>
</div>);}
})}
</div>
);
};
export default NotificationTable;
---> Axios request <---
export const getAllOpenNotification = ()=> api.get('/opennotifications');
--->Mongoose Query<---
getOpenNotification = async (req, res) => {
await Notification.find({Close:"1970-01-01T00:00:00.000Z"}, (err, notifications) => {
if (err) {
return res.status(400).json({ success: false, error: err });
}
if (!notifications.length) {
return res
.status(200)
.json({ success: true,data:[],error: `Open Notification not found` });
}
return res.status(200).json({ success: true, data: notifications });
}).catch(err => console.log(err));
};
--->Route<---*
router.get('/opennotifications',getOpenNotification);