skip to Main Content

i am not able to forward the response from the backend to the frontend.
i already define the request to check if a user exist based on his mail and password

this is how the server.js file look like

const router = express.Router();

// This section will help you get a list of all the records.
router.get("/", async (req, res) => {
  let collection = await db.collection("records");
  let results = await collection.find({}).toArray();
  res.send(results).status(207);
  
});

// This section will help you get a single record by id
router.get("/:email&:password", async (req, res) => {
  let collection = await db.collection("records");
 
  let query = { $and: [ {email: req.params.email} ,{password: req.params.password}] };
  
  let result = await collection.findOne(query);
  
  if (!result) {
    
    res.status(400).send('user not found')
    }
  else {
   
    res.status(200).send('user found')
  }
});

the frontend function that is supposed to catch the reponse is defined like this:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

async function onSubmit(e) {
   e.preventDefault();
   const editedPerson = {
    email: form.email,
    password: form.password,

   };

   
   const response = await fetch(`http://localhost:5050/login/${email.email}&${password.password}`);
   

  // console.log(response.status);

if(response.status == 200) {
  const record = await response.json();
  alert("user found");
  
  console.log(record);
  
}
else {
  alert("not found")
};

                                                      -

2

Answers


  1. Chosen as BEST ANSWER

    Thx a lot, i am getting now proper answer from the server side and i want to parse the returned data with the following code on the frontend side

     async function onSubmit(e) {
       e.preventDefault();
       const editedPerson = {
        email: form.email,
        password: form.password,
    
       };
    
       
     const response = await fetch(`http://localhost:5050/login/${email.email}&${password.password}`);
     //                           
     console.log(`http://localhost:5050/login/${email.email}&${password.password}`)
     console.log(response.status);
    
    if(response.status == 200) {
      const record = await response.json();
      alert("user found");
      
      // print the founded record
      console.log(record);
      
    }
    else {
      alert("not found")
    };

    but i am getting the follwong error in the console VM57:1 Uncaught (in promise) SyntaxError: Unexpected token 'u', "user found" is not valid JSON


  2. There are 2 ways for you too pass data via the url right now your in the middle of the 2

    1. part of the route

    /login/someName/somePassword

    to do it this way on the backend you just need to change

    router.get("/login/:email/:password", async (req, res) => {});
    

    and on the fronend change the url

    const response = await fetch(`http://localhost:5050/login/${email.email}/${password.password}`);
    
    1. As a query string

    /login?email=someEmail&password=somePassword

    to do it this way update the url in the backend

    router.get("/login", async (req, res) => {
        const {email, password } = req.query;
        //rest of code... 
    });
    

    and on the frontend

    const response = await fetch(`http://localhost:5050/login?email=${email.email}&password=${password.password}`);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search