undefined "Failed to fetch"

654 views Asked by At

I am using node with express and localhost server. I don't understand where's the problem when I try to fetch checkMail(),it shows alert failed to fetch and and error in browser console undefined: "Failed to fetch" I have added all my code below:

Server side code:

app.post('/checkMail',async function(req,res){
    var status;
    let Inputdata={email: req.body.Email};
    console.log(Inputdata.email)
    let selectQuery = `SELECT * FROM "schemaname"."tablename"  WHERE "EMAIL"='${Inputdata.email}';`

    let conn= await ibmdb.open(connStr);
    let results = await conn.query(selectQuery);
    console.log(results);
    if(results[0].EMAIL==Inputdata.email)
    {
        console.log("EMAIL IS THERE")
        let deta={success:0}
        console.log(deta)
        res.status(201).send({ data: deta});
    }
    else{
        console.log("new mail")
        let deta={success:1}
        console.log(deta)
        res.status(201).send({ data:deta });
    }
});

In terminal it shows the output by select query and also prints "EMAIL IS THERE".

client side code:

function checkMail()
{
    console.log("checkMail is called")
    let email = document.getElementById("inputEmail").value;
    let data = {Email:email}
    url=`http://${window.location.host}/checkMail`;
    let h = new Headers();
        h.append('Content-Type', 'application/json');
    let req = new Request(url, {
          method: 'POST',
          headers: h,
          body: JSON.stringify(data),
        });
          
          
          fetch(req)
          .then((res) => res.json())
          .then((content) => {
            //we have a response
            if ('error' in content) {
              //bad attempt
              failure(content.error);
            }
            if ('data' in content) {
              //it worked
              //callback(content.data);
              console.log(content);
            }
          })
          .catch(failure);
        
    
}

I guess the problem is at client side, can anyone please help me out. Thanks.

0

There are 0 answers