2

This is my response. that i am getting

{status: 'Success', length: 7, data: {…}}

This is an expanded version.

data:
tours: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
[[Prototype]]: Object
length: 7
status: "Success"

I wanted to loop over the tour property but doing a map is not going to work. I tried doing Object.entries() to convert it into an array but it is also not working. Can anyone suggest me a solution?

export const getData = () => {
  return async (dispatch) => {
    try {
      dispatch(isLoading(true));
      const res = await fetch(`http://localhost:5555/api/bc1/tours`);

      if (!res.ok) {
        throw new Error(`Error while connecting with server`);
      }

      const data = await res.json();
      dispatch(isLoading(false));
      dispatch(getTours(data));
    } catch (error) {
      dispatch(
        checkStatus({
          title: `Error`,
          message: error.message,
        })
      );
    }
  };
};

getTours is an empty array.

calling it in app.js

const dispatch = useDispatch();
  useEffect(() => {
    dispatch(getData());
  }, [dispatch]);

1 Answers1

2

No need to convert tours to object. You can do this:

const result = {status: 'Success', length: 7, data: {…}};
result.data.tours.map(function(item, i){
  // your code
})
Abasaheb Gaware
  • 509
  • 4
  • 13