Closures to get variable in node.js

Can anyone help me urgently for my Thesis software for get variable in closures..

this is my code in node.js

var kepala = express.basicAuth(authentikasi);
// authenticate for login 

function authentikasi(user, pass, callback) {
    // declrare my database mongodb
    db.collection('ak_teacher', function (err, data) {
        data.findOne({
            'tch_name': {
                '$regex': user
            }
        }, function (err, level) {

            console.log(level); // monitor data

            if (level == null) {
                console.log('Nilai database kepala sekolah masuk Null ulangi login');
                callback(null);
            } else {

                var a = level.tch_name;
                var b = level.tch_password;

                var c = level.sch_id; // (i need this variable for next code) 

                var result = (user === a && pass === b);
                console.log("id Sekolah : " + c);
                callback(null /* error */ , result);
            }
        });
    });
};

var tes = authentikasi(); // (in here i dont know declare for get my variable c)

app.get('/siswa_2', kepala, function (req, res) {
    // i use variable in here                            
    var sch_id = tes;
    console.log("id school in query :" + sch_id);
    console.log('Menampilkan Seluruh data Siswa');
    db.collection('ak_student', function (err, collection) {
        collection.find({
            "sch_id": sch_id
        }).toArray(function (err, items) {
            console.log(items);
            res.send(items);

        });
    });
});

Help me for get variable c i am confusion this 2 day... please thanks for u attention i am newbie in node.js Thanks..

You need to pass c into the callback as well to get it's value:

callback(null /* error */, result, c);

Then call authentikasi like this:

// Note, since this callback is called by passing "c"
// as the third argument, this is how we assign it to "tes"
//                                             |
//                                             |
//                                             V
authentikasi(user,pass,function(unused,result,tes) {
  app.get('/siswa_2', kepala, function(req, res) {
    var sch_id = tes;
    console.log("id school in query :" +sch_id);
    console.log('Menampilkan Seluruh data Siswa');
    db.collection('ak_student', function(err, collection) {
      collection.find({"sch_id":sch_id}).toArray(function(err, items) {
        console.log(items);
        res.send(items);
      });
    });
  });
});

You could also take advantage of locals:

Response local variables are scoped to the request, thus only available to the view(s) rendered during that request / response cycle, if any. Otherwise this API is identical to app.locals.

This object is useful for exposes request-level information such as the request pathname, authenticated user, user settings etcetera.

Assigning c to res.locals.c should let you use it in later elements of that particular request and subsequent response.

According to the docs, you're supposed to call back with a user object. You can simply put your c variable on there:

var kepala = express.basicAuth(authentikasi);

function authentikasi(user, pass, callback) {
    // declrare my database mongodb
    db.collection('ak_teacher', function (err, data) {
        data.findOne({
            'tch_name': {
                '$regex': user
            }
        }, function (err, level) {
            console.log(level); // monitor data

            if (level == null) {
                console.log('Nilai database kepala sekolah masuk Null ulangi login');
                callback(null);
            } else {
                var a = level.tch_name;
                var b = level.tch_password;
                var c = level.sch_id; // (i need this variable for next code) 

                if (user === a && pass === b) {
                    console.log("id Sekolah : " + c);
                    callback(null, {name: a, id: c});
//                                 ^^^^^^^^^^^^^^^^
                } else
                    callback(null, false);
            }
        });
    });
};
// don't try to authenticate outside of the request context

app.get('/siswa_2', kepala, function (req, res) {
    var sch_id = req.user.id;
//               ^^^^^^^^^^^
    console.log("id school in query :" + sch_id);
…
});