0

我正在开发论文软件以获取闭包中的变量。

这是我在 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);

        });
    });
});

我正在尝试获取变量c

4

3 回答 3

1

您还需要传递c给回调以获得它的值:

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

然后authentikasi像这样调用:

// 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);
      });
    });
  });
});
于 2013-07-23T13:15:07.603 回答
0

您还可以利用locals

响应局部变量的范围仅限于请求,因此仅可用于在该请求/响应周期中呈现的视图(如果有)。否则,此 API 与 app.locals 相同。

该对象对于公开请求级信息很有用,例如请求路径名、经过身份验证的用户、用户设置等。

分配cres.locals.c应该让您在该特定请求和后续响应的后续元素中使用它。

于 2013-07-23T13:31:55.163 回答
0

根据文档,您应该使用用户对象进行回调。你可以简单地把你的c变量放在那里:

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);
…
});
于 2013-07-23T14:07:41.973 回答