0

I am working on nodejs express and implement app.locals function

in view(ejs templete) file:-

<% if(someHelper('/admin/dashboard',req.user._id)){ %>
        <li>
            <a href="/admin/dashboard">
                <i class="fa fa-user">
                </i>
                <span>
                    Create Chairman
                </span>
            </a>
        </li>
<% } %>

in app.js(server.js) file:-

app.locals.someHelper = function(route_name,user_id) {

user.find({user_id:user_id, route_name:route_name}, function(err, user_per){
if(err) return false;
    if(typeof user_per != 'undefined' && user_per.length > 0){
            return true;
            console.log("true");
    } else {
        return false;
        console.log("false");
    }
});
}

when if condition is executed then not return permitted value 1.

also tried for take req and callback perameter and return the value but both are not useful in this function.

app.locals.someHelper = function(route_name, user_id, callback) {
user.find({ 
    user_id: user_id,
    route_name: route_name
}, function(err, user_per) {

    if(typeof user_per != 'undefined' && user_per.length > 0){
        // permitted
        return callback(1);
    }

    callback(0);
});
}

callback is not a function

i want return permitted 0 if portion is not executed else return permitted 1.

reference

Amit Patel
  • 167
  • 2
  • 12
  • Can you share a bit more of your code? `req` is undefined, but without seeing more, I can't know why – Alicia Sykes Apr 29 '19 at 06:13
  • when use like that :- app.locals.someHelper = function(route_name,user_id,callback) { var permitted = 0; user.find({user_id:user_id,route_name:route_name},function(err,user_per){ if(typeof user_per != 'undefined' && user_per.length > 0){ permitted = 1; return callback(permitted); } }); return callback(permitted); } given this error callback is not function – Amit Patel Apr 29 '19 at 06:42
  • That's not very readable Format it up, and add to your question – Alicia Sykes Apr 29 '19 at 06:53
  • okkk, i done ... – Amit Patel Apr 29 '19 at 06:59
  • Ah, I see the problem, your just forgetting to pass all params to `someHelper`, the 3rd param must be your callback, and it's not there. Hope that helps :) – Alicia Sykes Apr 29 '19 at 07:02
  • How can i implement please give me the example in answer – Amit Patel Apr 29 '19 at 07:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192550/discussion-between-amit-patel-and-alicia). – Amit Patel Apr 29 '19 at 07:08
  • Possible duplicate of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call . The answer is that you can't return a value from async function. You could do this with promises but EJS seems to not support them. Don't use a helper or switch to view engine that supports promsies (e.g. Nunjuck). – Estus Flask Apr 29 '19 at 07:14
  • Any another way to do this function? – Amit Patel Apr 29 '19 at 07:18

1 Answers1

1

Showing "callback is not a function" is because of you are not defined callback function. So you please define the function like

function callback(value){
    console.log(value)
}

then you can pass the callback to your function as

app.locals.someHelper(..,...,callback);

Let me give simple example in snippet, you can look at it.

helper = function(val, callback){
    if(val === 1)
        callback(true)
    else 
        callback(false)
}

function callback(value){
    console.log(value)
}

helper(1, callback);
helper(2, callback);
app.locals.someHelper = function(route_name,user_id) {
var permitted = 0;
user.find({user_id:user_id, route_name:route_name}, function(err, user_per){
if(err) return false;
if(typeof user_per != 'undefined' && user_per.length > 0){
        return true;
    }
else {
    return false;
    }
});

}

look at this

Nikhil Unni
  • 739
  • 5
  • 12
  • But how to give callback in ejs templete – Amit Patel Apr 29 '19 at 08:42
  • It's quite simple. do a little change in your code. 1. In if condition return true instead of 1, 2. else condition for the if statement must write in the same function. Please look at the answer below the snippet for an example – Nikhil Unni Apr 29 '19 at 09:03
  • And remind one thing, you should pass user id to ejs template from where you called the render function. Otherwise it show "req.user_id not defined" – Nikhil Unni Apr 29 '19 at 09:10
  • this function not return anything true or false chack it out bro – Amit Patel Apr 29 '19 at 09:23
  • Bro, please look at the picture that I added in the answer. you can see there a **create chairman** link in the bottom of that image. That is obtained by executing the code. Hence solved – Nikhil Unni Apr 29 '19 at 09:32
  • can you please share your updated ejs file and server file with me ? – Nikhil Unni Apr 29 '19 at 09:37
  • okk,bro i edit my question as per updated ejs and app.js file – Amit Patel Apr 29 '19 at 09:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192558/discussion-between-amit-patel-and-nikhil-unni). – Amit Patel Apr 29 '19 at 09:49
  • 1.define callback also using app.locals, 2. in ejs file and app.js file, pass callback as 3rd argument, – Nikhil Unni Apr 29 '19 at 09:49