【问题标题】:Closure find node.js, mongodb, express闭包查找node.js、mongodb、express
【发布时间】:2013-08-01 21:27:12
【问题描述】:

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

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)
        });
    });
};

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

【问题讨论】:

  • 我认为您无法使用 post 方法重定向(如果我了解您在寻找什么)。如果你想传递变量使用会话,像这样:stackoverflow.com/questions/5599083/…
  • 感谢您的关注,但我的代码没有得到答案...我用于搜索,但我的代码使用 html 中的帖子,因此数据不会显示在 get..
  • 你不能用这种方式初始化test变量。 a 将未定义。
  • 是的,在我的代码中有 2 个方法 post 和 get,输入数据使用 post 和显示数据使用 get..但是我无法关闭 post 方法中的变量,而 get 方法中没有..
  • 使用会话,在后期存储变量并在获取阶段检索它。

标签: node.js mongodb express closures


【解决方案1】:
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)
        });
});

};

【讨论】:

  • 您好,感谢您的帮助,我明白了,兄弟,我的数据不在变量 a 中,我在查找数据中更改了代码使用 db.collection('ak_test_score', function(err, collection) {collection .findOne({'std_id': {'$regex': id_school}}, function (err, level) { 和它的工作谢谢你
  • 但我有新问题大声笑.. 如果我的搜索 = null 我的 node.js 是堆栈...我使用 if (level = null) { }else{} 我的应用程序。未定义 T_T hik hik
猜你喜欢
  • 2017-01-21
  • 2013-05-14
  • 1970-01-01
  • 2013-06-03
  • 2014-01-24
  • 2014-03-20
  • 1970-01-01
  • 2021-07-20
  • 2016-02-03
相关资源
最近更新 更多