如何返回我从mongoose查找获得的EJS模板中的值

时间:2017-11-01 05:01:36

标签: node.js mongodb ejs

我需要在EJS模板中返回值。所以我需要在mongoose之外返回mongoose查找的结果。我试图在全局声明响应变量但它不起作用。如果我在mongoose里面使用它的工作正常并且我在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(响应); //没有得到结果 }

给我一​​些建议来解决这个问题

0 个答案:

没有答案