Home > Mobile >  React JS - Expected to return a value at the end of arrow function ES lint Error
React JS - Expected to return a value at the end of arrow function ES lint Error

Time:03-03

I'm checking if the env is development or prod, If development I'm returning mock data else I'm making an API call to fetch data.

Now I'm getting this ES lint Error,

Expected to return a value at the end of arrow function.

What I'm doing wrong here ? please help

export const getData = (request: any) => {
  if (process.env.NODE_ENV !== 'development') {
    axios.post(EMAIL_DISPUTE_API, { request })
      .then((res) => {
        return res.data;
      })
      .catch((e) => {
        console.log(e);
      });
  } else {
    return emailDisputeMockResponse;
  }
};

CodePudding user response:

Requires return statements to either always or never specify values.

Try without else bloc

export const getData = (request: any) => {
  if (process.env.NODE_ENV !== 'development') {
    axios.post(EMAIL_DISPUTE_API, { request })
      .then((res) => {
        return res.data;
      })
      .catch((e) => {
        console.log(e);
      });
  }
    return emailDisputeMockResponse;
};

CodePudding user response:

in case of your function,if block don't return value but else return emailDisputeMockResponse, so you need declare return value type for your function and return value in the catch block, just like following code snippet

export const getData = async (request: any): Promise<emailDisputeResponse | null> => {
  let result: emailDisputeResponse | null
  if (process.env.NODE_ENV !== 'development') {
    result = await axios.post(EMAIL_DISPUTE_API, { request })
      .then((res) => {
        return res.data;
      })
      .catch((e) => {
        console.log(e);
        return null;
      });
  } else {
    result = emailDisputeMockResponse;
  }
  return result
};
  • Related