1

The following client code calls a function

export async function verifyImage (image ) {

  const data = new FormData();
  ...
    
    let moderated;
    
          await verifyImage (image).then(function (response) {
            moderated = response;
          });
    
          console.log(moderated); //why is this always undefined?

and the function code is

  await axios({
    method: 'post',
    url:'https://api.someapi.com/1.0/check.json',
    data: data,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  .then(function (response) {
    // on success: handle response
    console.log("resp in moderate pics")
    console.log(sendVerifyResponse (response.data))
    const moderated = sendVerifyResponse (response.data);
    return moderated;
  })
  .catch(function (error) {
    // handle error
    if (error.response) console.log(error.response.data);
    else console.log(error.message);
    return false;
  });

The following log shows the correct values

console.log(sendVerifyResponse (response.data)) 

But return moderated; is always undefined.

Kal
  • 1,656
  • 4
  • 26
  • 41
  • How do you pass `moderated` from get function to where that variable is called? You say first block of code call is, but you set `moderated` again. This is react just pass it as props, if it is in another file.... – SinisaM Dec 30 '22 at 21:37
  • @SinisaM `return moderated;` statement – Kal Dec 30 '22 at 22:15

0 Answers0