Throws cannot use 'in' operator to search for 'validateStatus' error

Viewed 1951

On click on a button I would to receive token in the client side, but I am getting below error in console TypeError: Cannot use 'in' operator to search for 'validateStatus' in sample1@test.com at merge. Could someone please advise what is casing the error.

client side:

const validateLink = () =>{
        const fetchData = async () => {
            try {
                const email = localStorage.getItem("userEmail");
                const res = await Axios.get('http://localhost:8000/service/validatelink', email);
                if (res.data) {
                    console.log("Link token created:" + res.data);
                }
            } catch (e) {
                console.log(e);
            }
        }
        fetchData(); 
    }

server.js

app.get('/service/validatelink', async (req, res) => {
  try {
    const userEmail = req.body.email;
    const data = await LinkTokenModel.findAll({ where: { email: userEmail } });
    
    if(currentDate < data.expiredAt ){
      let tokendata = data.token;
      res.status(200).send(tokendata);
    } else {
      res.status(404).json({ success: true });
    }
     
  } catch (e) {
    res.status(500).json({ fail: e.message });
  }
});

TypeError: Cannot use 'in' operator to search for 'validateStatus' in sample1@test.com at merge (mergeConfig.js:67) at Object.forEach (utils.js:247) at mergeConfig (mergeConfig.js:64) at Axios. [as get] (Axios.js:76) at Function.wrap [as get] (bind.js:9) at fetchData (createLink.js:37) at validateLink (createLink.js:45) at HTMLUnknownElement.callCallback (react-dom.development.js:3945) at Object.invokeGuardedCallbackDev (react-dom.development.js:3994) at invokeGuardedCallback (react-dom.development.js:4056) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)

1 Answers

You should be using post to send data to server. Also you should be sending an object with email property in order to access it like req.body.email on the server.
Your code should probably be something like:

const email = localStorage.getItem("userEmail");
const res = await Axios.post('http://localhost:8000/service/validatelink', {email});
//--------------------------------------------- {email: "johndoe@gmail.com} --^

In this case server needs to be updated to app.post('/service/validatelink'


If the request has no side effect and you meant to use a get, it should be like:

axios.get('/http://localhost:8000/service/validatelink?email='+email)

Or

axios.get('/http://localhost:8000/service/validatelink',{
  params: {
    email
  }
})
Related