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);
请发布集合中的一个文档。请发布集合中的一个文档。有时会发生…:)有时会发生…:)