簡體   English   中英

MongoDB findOne在node.js應用程序中第二次失敗...連接到以下錯誤:mongodb://

[英]MongoDB findOne failing SECOND TIME in node.js app… ERROR connecting to: mongodb://

這是在node.js應用程序上發生的。

我試圖查看hrefs數組中的食譜URL之一是否在MongoDB中存在。 以下代碼在首次運行時有效,但在30秒后再次運行(setInterval)時失敗。

第一次成功為數組中的每個URL返回yes或no:

yes
no
yes
yes
yes
yes

三十秒后,當它第二次運行時:

ERROR connecting to: mongodb://<the url here>. Error: failed to connect to [<the url here]]
ERROR connecting to: mongodb://<the url here>. Error: failed to connect to [<the url here]]
ERROR connecting to: mongodb://<the url here>. Error: failed to connect to [<the url here]]
ERROR connecting to: mongodb://<the url here>. Error: failed to connect to [<the url here]]
ERROR connecting to: mongodb://<the url here>. Error: failed to connect to [<the url here]]

編碼:

var mongoDB;
function connectToDb(done){
  mongo.connect(uristring, options, function (err, db) {
      if (err) {
         console.log ('ERROR connecting to: ' + uristring + '. ' + err);
      } else {
         console.log ('Succeeded connected to: ' + uristring);
         mongoDb = db;
         done();
      }
  });
}

function checkHREFS(recipes) {
   var recipeCollection = mongoDb.collection('recipeURL');
   recipefCollection.findOne({
      'recipe_url': { $in: recipes }
   }, function (err, recipe) {
      if (err) {
         console.log('ERROR:' + err);
      } else if (!href) {
         console.log('no');
      } else {
         console.log('yes');
         return true;
      }
   });
}

connectToDb(function(){
   checkHREFS(hrefs);
});

更新:這沒有幫助,並且if(mongDB)中的console.log從未被調用。

var mongoDB;
function connectToDb(done){
   if(mongoDB) { console.log('already connected'); return done(); }
   mongo.connect(uristring, options, function (err, db) {
      if (err) {
         console.log ('ERROR connecting to: ' + uristring + '. ' + err);
      } else {
         console.log ('Succeeded connected to: ' + uristring);
         mongoDb = db;
         done();
      }
  });
}

我得到了很多錯誤,可以在以下兩個錯誤之間切換:

ERROR connecting to: <url here>. Error: failed to connect to [<number here>.mongolab.com:<port here>]

ERROR connecting to: <url here>. Error: No valid replicaset instance servers found

我在同一文件中擁有的另一個功能可以正常工作:

connectToDb(function(){
   recipesToDB(recipes);
});
function recipesToDB(recipes) {
   mongoDb.createCollection('recipes', function(err, collection) {});
   var recipeCollection = mongoDb.collection('recipes');

   recipeCollection.insert(recipes, {continueOnError: true}, function(err, result) {
      if (err) {
         console.log('ERROR:' + err);
      } else {
         console.log('*******************************************');
         console.log('********Succeeded inserted recipes********');
         console.log('*******************************************');
      }
   });
}

按您的要求。 將其更改為myDB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM