Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongo:find不返回任何内容,也不返回任何错误_Javascript_Mongodb - Fatal编程技术网

Javascript Mongo:find不返回任何内容,也不返回任何错误

Javascript Mongo:find不返回任何内容,也不返回任何错误,javascript,mongodb,Javascript,Mongodb,我只是成功地将连接到服务器,然后什么都没有,没有输出,甚至没有错误。我做错了什么?您无法调用该函数 // Use connect method to connect to the server MongoClient.connect('mongodb://localhost:27017/products', function (err, db) { assert.equal(null, err); console.log("Connected successfully to server

我只是成功地将
连接到服务器
,然后什么都没有,没有输出,甚至没有错误。我做错了什么?

您无法调用该函数

// Use connect method to connect to the server
MongoClient.connect('mongodb://localhost:27017/products', function (err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  var findDocuments = function (db, callback) {
    // Get the documents collection
    var collection = db.collection('products');
    // Find some documents
    collection.find(
      { $and: [{
        "qty": {
          $gt: 0
        }
      }, {
        "price": {
          $gte: 2000.0
        }
      }]
      },
      { "name": 1,
        "brand": 1,
        "_id": 0
      }).toArray(function (err, docs) {
      assert.equal(err, null);
      console.log('Found docs');
      callback(docs);

      db.close();

    });
  }

});

function callback(docs) {
  console.log('callback called');
  console.log(docs)
}
函数应如下所示:

findDocuments(db, callback);

您缺少对函数的调用

// Use connect method to connect to the server
MongoClient.connect('mongodb://localhost:27017/products', function (err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  var findDocuments = function (db, callback) {
    // Get the documents collection
    var collection = db.collection('products');
    // Find some documents
    collection.find(
      { $and: [{
        "qty": {
          $gt: 0
        }
      }, {
        "price": {
          $gte: 2000.0
        }
      }]
      },
      { "name": 1,
        "brand": 1,
        "_id": 0
      }).toArray(function (err, docs) {
      assert.equal(err, null);
      console.log('Found docs');
      callback(docs);

      db.close();

    });
  }

});

function callback(docs) {
  console.log('callback called');
  console.log(docs)
}
函数应如下所示:

findDocuments(db, callback);

请发布集合中的一个文档。请发布集合中的一个文档。有时会发生…:)有时会发生…:)