Adam Saleem
Adam Saleem

Reputation: 93

Database returning undefined results when I query the DB using NodeJS

I am trying to access data from my MySQL database and compare that data to my form data to authenticate sign in process. But the problem I am facing is that my database query is returning undefined values in the db.query() function. I have attached the code of my index.js below:

app.post("/api/get/signin", (req,res)=>{
    
    const userid = req.body.userId;
    userIdG = userid;
    const password = req.body.password;
    const consumerKey = req.body.consumerKey;
    const sqlSelect =
    `SELECT userid FROM signup WHERE userid = ? AND password = ? AND consumerKey = ?`;
     db.query(sqlSelect,[userid , password , consumerKey], (err, result)=> {
        console.log(result.userid);
        console.log(err);
        //console.log(err);
        if(result.userid == userid && result.password == password && result.consumerKey == consumerKey)
        {
            //res.send(result);
            console.log("Successfully Login");
            auth = true;
        }
        else
        {
            console.log('Local');
            console.log(userid);
            console.log(password);
            console.log(consumerKey);
            console.log("UnSuccessfully Login");
            auth = false;
        }
         
    })
})

Undefined data returned from the database

Upvotes: 0

Views: 172

Answers (1)

robertklep
robertklep

Reputation: 203574

result will be an array (because queries can have multiple results), so to access the userid (of the first result, provided that there is one!):

console.log(result[0].userid)

Upvotes: 2

Related Questions