0

我正在尝试将字符串从数组保存到集合中,如果之前不存在,我将字符串保存在集合中,但是在保存字符串之后,这是 null

这是我的源代码:

exports.procc = function(req, res) {
         //combinatorial is the array that contains the strings
            var k = combinatorial.length, m = 0;
            var collection = db.collection('searchtb');
            collection.count({'search': b}, function(err, count) {

              for (m = 0; m < k; m++) {
                                        //Here prints the value of the varible
                                        console.log(combinatorial[m]);
                                        //Here the same variable is null
                                        collection.count({'search': combinatorial[m]}, function(err, count) {
                                              c=count;
                                              if(c==0){
                                               collection.insert({'search': combinatorial[m],'hits':1}); 
                                              }                                        
                                         });
                                    }

            });
     res.end();
    };

但是,如果我编写以下代码而没有“计数”,则字符串以正确的方式保存。

 for (m = 0; m < k; m++) {
                          collection.insert({'search': combinatorial[m],'hits':1}); 
                         }

我不知道我的错误是什么。谢谢你的帮助

4

0 回答 0