node.js - 闭包查找node.js、mongodb、express

标签 node.js mongodb express closures

请帮助我。我的搜索使用帖子中需要变量:

app.post('/find', function(req, res) {
    var id_school = req.body.std_id;
    console.log('show '+ id_sekolah);
    db.collection('ak_test_score', function(err, collection) {
        collection.find({'std_id':id_school}).toArray(function(err, level) {
            var a = level.std_id;
            var b = level.school_name;
        });
    });
    res.redirect('/test_score'); // send to my page to get
};

var test = a;  // not defined variable a not have

app.get('/test_score', function(req, res) {
    var id_school = test;
    console.log('show '+ id_sekolah);
    db.collection('ak_test_score', function(err, collection) {
        collection.find({'std_id':id_school}).toArray(function(err, level) {
             res.send(level)
        });
    });
};

我使用它来使用帖子进行网站搜索。

最佳答案

app.post('/find', function(req, res) {
   var id_school = req.body.std_id;
   console.log('show '+ id_sekolah);
   db.collection('ak_test_score', function(err, collection) {
      collection.find({'std_id':id_school}).toArray(function(err, level) {
        var a = level.std_id;
        app.set('data',a);
        var b = level.school_name;

        });
});
   res.redirect('/test_score'); ///// send to my page to get
};


 app.get('/test_score', function(req, res) {
    var id_school = app.get('data');
    console.log('show '+ id_sekolah);
    db.collection('ak_test_score', function(err, collection) {
      collection.find({'std_id':id_school}).toArray(function(err, level) {
           res.send(level)
        });
});

};

关于node.js - 闭包查找node.js、mongodb、express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17985887/

相关文章:

javascript - MEAN 堆栈,我不想使用 Jade,我怎么只使用 HTML

javascript - 从文件夹导入和导出所有 .vue 文件

javascript - ShardingTest() 可以将非默认的 dbpath 传递给 mongod 吗?

node.js - 如何在surge.sh上部署Express应用程序

javascript - Node.js 似乎在循环后没有释放内存

node.js - Mongoose 改变模式格式

java - 嵌套 Json 聚合

javascript - Mongoose -> findOne -> 排序 -> 如果数据库中没有记录

javascript - 将脚本 src 传递到 Pug 模板中

Node.js 和 Express.js 布局