I'm trying to send API with axios to check if login creds are correct, and I can't retrieve the result from the API. the mongodb .find finds the correct row but then i want to compare the result from mongo to the api parameters. for example: I send to the API {email:[email protected], password: somthing} and compare with the row {id..,email:somthing, pass: somthing}
code: API
export default async function handler(request, response){
try{
const { mongoClient } = await connectedToDatabase();
const db = mongoClient.db("db");
const table = db.collection("users");
const {email,password} = request.body
const result = await table
.find({email})
.limit(1)
.toArray();
if (result[0].password == password){
console.log('pass correct')
}
else{
console.log('false');
}
response.status(200).json(result);
}
catch(e){
console.error(e);
}
}
Request:
const clickLogin = async (e) => {
const registerDetails = {
'email': email,
'password': password
};
e.preventDefault();
const res = await axios ({
method: 'POST',
headers:{ 'Content-Type': 'application/json'},
url: 'api/list',
data: JSON.stringify(registerDetails),
});
if (res.status == 200){
console.log(res);
console.log(res.data[0].NickName);
}
setNickName('');
setEmail('');
setPassword('');
};
CodePudding user response:
I managed to find the value of object by ['key'] and then compared it. code:
export default async function handler(request, response){
try{
const { mongoClient } = await connectedToDatabase();
const db = mongoClient.db("db");
const table = db.collection("users");
const {Email,password} = request.body
const result = await table
.find({Email})
.limit(1)
.toArray();
console.log(result[0]);
if (result[0] !== undefined){
if(result[0]['password'] === password){
console.log('True welcome!');
response.status(200).json(result);
}
else{
console.log('User/Pass not exist!.');
response.status(400).json({ msg: "Email or Password are not exist!" });
}
}
else{
console.log('User/Pass not exist!.');
response.status(400).json({ msg: "Email or Password are not exist!" });
}
}
catch(e){
console.error(e);
}
}