read mongodb result with api parameter given

Viewed 15

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:somthing@sms.com, 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("pmmbrp");
        const table = db.collection("pmmbrps");
        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('');
      };
0 Answers
Related