Версия NodeJs = 8.9.4;
Версия MongoDB-server = 3.0;
Версия модуля = 3.0;
Есть такой код подключения и поиска в БД, который периодически
не работает :
const mongoClient = require('mongodb').MongoClient;
const mongodb = new mongoClient("mongodb://localhost:27017/");
var baza = "test";
var collectionName= "goods";
function findInDb(data, callback){
// открываю подключение к БД
console.log("*****до mongodb.connect******");
mongodb.connect((err, client)=>{
if (err) throw err;
var db = client.db("goods");
//подключаюсь к коллекции
console.log("*****до db.collection******");
db.collection(collectionName,(err, collection) =>{
if (err) throw err;
//запускаю поиск документов соответствующих критерию поиска
console.log("*****до collection.find******");
console.log("data", JSON.stringify(data));;
collection.find(data, {projection:{id:1}}).sort(sortValue).skip(skip).limit(limit).toArray((err, result)=>{
if (err) throw err;
//теперь необходимо узнать кол-во документов соответствующих критерию поиска (для пагинации)
console.log("*****до collection.count******")
collection.count(data,(err, count) =>{
if (err) throw err;
var f ={};
f.result = result;
f.count = count;
client.close();
console.log("*****client.close()******")
return callback(f);
})
})
})
})
}
Когда все норм консоль выдает следующее (как и ожидается):
до mongodb.connect
до db.collection
до collection.find
data: {"$and":[{"visible":true},{"id":{"$in":[16]}}]}
до collection.count
Result: [id: 16]
Count: 1
client.close();
а когда работать не хочет - выбрасывает исключение и еще какие-то непонятные мне надписи):
до mongodb.connect
the options [socketOptions] is not supported
the options [read_preference_tags] is not supported
the options [dbName] is not supported
the options [servers] is not supported
the options [server_options] is not supported
the options [db_options] is not supported
the options [rs_options] is not supported
the options [mongos_options] is not supported
до db.collection
до collection.find
data: {"$and":[{"visible":true},{"id":{"$in":[16]}}]}
C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\utils.js:132
throw err;
^
MongoError: Topology was destroyed
at nextFunction (C:\Users\user\Desktop\Back-end\node_modules\mongodb-core\lib\cursor.js:588:27)
at Cursor.next [as _next] (C:\Users\user\Desktop\Back-end\node_modules\mongodb-core\lib\cursor.js:791:3)
at fetchDocs (C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\cursor.js:931:10)
at toArray (C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\cursor.js:957:3)
at executeOperation (C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\utils.js:409:22)
at Cursor.toArray (C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\cursor.js:917:10)
at db.collection (C:\Users\user\Desktop\Back-end\myModules\findIndb.js:38:93)
at Db.collection (C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\db.js:445:21)
at mongodb.connect (C:\Users\user\Desktop\Back-end\myModules\findIndb.js:35:7)
at args.push (C:\Users\user\Desktop\Back-end\node_modules\mongodb\lib\utils.js:405:72)
Program node .\app.js exited with code 1
Подскажите - в чем проблема?
P.S. После тестов определил, что проблема возникает, только если функцию запустить несколько раз подряд, например в цикле. Если запускать ее 1 раз и дожидаться выполнения, то все работает норм.
is not supported
?)