1

I want to display User Verification menu only if user is not verified in node js

header.ejs
<% if(!isVerified(user.id)){%>
    <li><a href="/auth/logout">User Verification</a></li>
<% } %>

function.js

module.exports.isVerified = function(user_id) {
  return new Promise(function(resolve, reject) {
    var sql = 'SELECT * FROM `users` WHERE id='+mysql.escape(user_id)+' and verified=1';
    config.db.query(sql,function (err, rows){
      if(err){
        reject(false);
      }else{
        resolve(rows);
      }
    });  
  });
}

but it is not showing the menu....what should i do? I don't know i'm new in nodejs. Thanks in advance

Sagar Gor
  • 111
  • 3
  • 13

5 Answers5

1

Finally after a week i have solved this problem

function.js (same as i have posted in question)

module.exports.isVerified = function(user_id) {
  return new Promise(function(resolve, reject) {
    var sql = 'SELECT * FROM `users` WHERE id='+mysql.escape(user_id)+' and verified=1';
    config.db.query(sql,function (err, rows){
      if(err){
        reject(false);
      }else{
        resolve(rows);
      }
    });  
  });
}

now the change that i have done here is in app.js and header.ejs file.

app.js

const fn = require('./helper/function_helper')
app.use(async function(req, res, next) {
  if(req.session.user.id){
    res.locals.user = await fn.isVerified (req.session.user.id);
    //so now i can access user.verified in header.ejs file.
  }
  next();
});

header.ejs

//instead of using the isVerified() function I have used the user.verified
<%if(user.verified==0){%>
     <li><a href="/home/user-verification">User Verification</a></li>
<%}%>
Sagar Gor
  • 111
  • 3
  • 13
0

Your promise doesn't return a boolean, it wait and indicate if the SQL query worked or not, you need to return a boolean in both case.

KiraLeOuf
  • 23
  • 6
  • i have changed the code like this `if(rows.length>0){ resolve(true); }else{ resolve(false); }` still it doesn't work – Sagar Gor Jan 28 '20 at 10:28
0

Since you are using a promise, you are dealing with asynchronous programming. What that means is that, your returns are unpredictable. How you should fix that is defining promise separately and check it with a code block similar to .then(function(res){...})

I recommend you to look at these resources:

  1. Javascript Promises
  2. How to return inside promise?
Atakan Atamert
  • 215
  • 2
  • 13
0

use a module for node that provide synchronous functions. Here you'll find a module that provide sync/async functions to deal with mysql: node-mysql-libmysqlclient

    //Sample code
    module.exports.isVerified = function (user_id) {

    var config = require("./config.json") ;
    var mysql = require('mysql-libmysqlclient') ;
    var client = mysql.createConnectionSync(config.host, config.user, config.password, config.database) ;
    var flag = false;
    var query = 'SELECT * FROM `users` WHERE id=' + mysql.escape(user_id) + ' and verified=1' ;
    var handle = client.querySync(query) ;
    var results = handle.fetchAllSync() ;
    if (results.length > 0)
    {
        flag = true;
    } else
    {
        flag = false;
    }
    console.log(JSON.stringify(results)) ;
    return flag;

}
Sohan
  • 6,252
  • 5
  • 35
  • 56
  • `UnhandledPromiseRejectionWarning: TypeError: config.db.query(...).then is not a function` i'm getting this error now – Sagar Gor Jan 28 '20 at 10:53
  • right, In that case would suggest using different library. Have a look at this. If this fine I will update the code https://github.com/Will-I4M/node-mysql-libmysqlclient Also why you need promise block in that case, could you not just have function without promise block? – Sohan Jan 28 '20 at 10:54
  • can i do this without promise and then,catch block? – Sagar Gor Jan 28 '20 at 11:04
  • Not sure, you need to give a try. Updated solution – Sohan Jan 28 '20 at 11:08
  • Do you want to try with different library? – Sohan Jan 28 '20 at 11:59
0

I don't know if this fix your problem, but your ejs code is wrong. You have not closed the ejs tags. This is the proper syntax

<% if(!isVerified(user.id)){ %>
    <li><a href="/auth/logout">User Verification</a></li>
<% } %>
dimitris tseggenes
  • 3,031
  • 2
  • 16
  • 23