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
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
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
There are 2 ways for you too pass data via the url right now your in the middle of the 2
to do it this way on the backend you just need to change
and on the fronend change the url
to do it this way update the url in the backend
and on the frontend