0

I need to return the value in EJS Template.. So I need to return the results outside mongoose which i got from mongoose lookup .I have tried to declare the response variable globally but its not working . If I used inside the mongoose its working fine And I am getting the results in Array [false,true,true]

app.locals.myrole_check = function(role_master,page_name,callback){
    var id =  ObjectId(role_master.id);
    var response = [];
      mongo.user.aggregate([

            { $lookup:
                {
                from: 'roles',
                localField: 'role_type',
                foreignField: 'role_name',
                as: 'rolesforusers'
                }
            },

    { $unwind: "$rolesforusers" },

    { $match: { '_id':  id}},


    {"$project": {

      "name": "$rolesforusers.role_name",
      "role": "$rolesforusers.role"
    }}

    ]) .exec(function(err, result){
        var response = [];
        add_role =[];
        edit_role =[];
        delete_role =[];
 if(result[0]){
        if(result[0].role.length!=0)
            {
            if(result[0]['role'].add)
            {
            add_role =  result[0]['role'].add.indexOf(page_name) > -1;
            }
            else {
            add_role = false;
            }
            if(result[0]['role'].edit)
            {
            edit_role =  result[0]['role'].edit.indexOf(page_name) > -1;
            }
            else {
            edit_role = false
            }
            if(result[0]['role'].del)
            {
            delete_role =  result[0]['role'].del.indexOf(page_name) > -1;
            }
            else {
            delete_role =false
            }

        }
     else if(result[0].role.length==0) {

            add_role = true;
            edit_role = true;
            delete_role = true;

        }
 response.push(add_role);
 response.push(edit_role);
 response.push(delete_role);
 console.log(response);
 callback(response);
 }
    });

console.log(response); // I need to get the value here and have to return because i have to use the values in ejs template
}

app.locals.myrole_check1 = function(role_master,page_name,callback){

console.log(response); // Not getting the result here }

Give me some suggestion to get resolve this issue

Vishnu
  • 745
  • 12
  • 32
  • You need to work "inside" the callback response. Other code still executes while async methods "do their thing". Read the material in the answers, practice some things and let it sink in. – Neil Lunn Nov 01 '17 at 05:08
  • I have tried call back but i don't know how to do exactly ,Because am new to this technology – Vishnu Nov 01 '17 at 05:10
  • That's why you have been pointed to very descriptive answers, in order to learn. – Neil Lunn Nov 01 '17 at 05:11
  • Yes, I have tried call back also,Now i have updated my questions with callback ,But its not working – Vishnu Nov 01 '17 at 05:27
  • You have not "upated" anything. and clearly don't understand what "callback" means. This is why you need to grab your favorite beverage and snacks, sit down for a few hours and "actually read the answers given many times before" to questions just like what you are asking. Your `console.log()` is in the wrong place. Your "external variable" is the wrong thing to do. All of this knowledge contained in answers provided by people who already spent there good time providing them. Please stop commenting and "go and read and learn". That's what the links on top of your question are for. – Neil Lunn Nov 01 '17 at 05:31
  • Thank You for your response.. I will learn – Vishnu Nov 01 '17 at 05:35

0 Answers0