簡體   English   中英

閉包以獲取node.js中的變量

[英]Closures to get variable in node.js

我正在研究在閉包中獲取變量的論文軟件。

這是我在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

您還需要將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);
      });
    });
  });
});

您還可以利用當地人

響應局部變量的作用域是請求,因此僅可用於在該請求/響應周期中呈現的視圖(如果有)。 否則,此API與app.locals相同。

該對象對於公開請求級別的信息很有用,例如請求路徑名,經過身份驗證的用戶,用戶設置等。

c分配給res.locals.c應該可以在特定請求和后續響應的后續元素中使用它。

根據docs ,您應該使用一個用戶對象進行回調。 您可以簡單地將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);
…
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM