微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nodejs Mongodb MongoNetworkError: 无法连接到服务器 [localhost:27017]

如何解决Nodejs Mongodb MongoNetworkError: 无法连接到服务器 [localhost:27017]

我正在使用 nodejs、ejs、mongoDB、ffmpeg 和 chokidar。这是侦听文件夹并添加删除mongodb。如果我在 5 个文件夹中运行。完成。工作非常好。但我在 1000 文件夹中运行。它不起作用。我收到错误。我试过了。但我没有修复它。

这段代码是mongodb连接和craete:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url,function(err,db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

文件夹读取和数据库追加的代码

var watch1watcher = chokidar.watch('Z:\\watch1\\watch_1\\',{ignored: /^\./,persistent: true});
watch1watcher
  .on('add',function(path) {
    MongoClient.connect(url,db) {
      if (err) throw err;
      const findway = path.split('\\');
      const datefind = findpath[7]
      const word = '_'
      if(findpath[8].includes(word) ){
        const datefindundu = datefind.split('_')
        const yenidatefind = datefindundu[1]
        const Nowdate = yenidatefind.split('.')
        const day = Nowdate[0]
        const month = Nowdate[1]
        const findclock = findpath[8]
        const finderclock = findclock.split('_')
        const findedclock = finderclock[1].split('.')
        const findclc = findedclock[0]
        const findwo = findclc.split('-')
        const clock = findwo[0]+':'+findwo[1]+':'+findwo[2]
        const nextfind = parseInt(findwo[0]) + 1
        if(nextfind > 23){
          nextclock = '00'+':'+findwo[1]+':'+findwo[2]
        }else if(nextfind<10){
          nextclock = '0'+nextfind +':'+findwo[1]+':'+findwo[2]
        }else if(nextfind>9){
          nextclock = nextfind +':'+findwo[1]+':'+findwo[2]
        }

          // Video Thumbnail
          const resimadi = findway[8].split('.');
          ffmpeg({ source: path })
          .on('filenames',(filenames) => {
              console.log('Created file names',filenames);
          })
          .on('end',() => {
              console.log('Job done');
          })
          .on('error',(err) => {
              console.log('Error',err);
          })
          .takeScreenshots({
              size: '500x250',filename: day+month+findway[5]+resimadi[0],timemarks: [ 2 ]
          },'public/images/thumbnail');
          vthumnail = day+month+findway[5]+resimadi[0]+'.png'

           var myobj = { channel_name: findway[4],year: findway[5],month: month,day : dmonth,start_time : clock,end_time : nextclock,videoPath : path,vthumbnail : vthumnail  };
            console.log(myobj)
        }
     
      var dbo = db.db("mydb");
      dbo.collection("watch1").insertOne(myobj,res) {
        if (err) throw err;
        console.log("1 document inserted");
        db.close();
      });
    });

这是错误信息:

> MongoNetworkError: Failed to connect to server [localhost:27017] on
> first connect [MongoNetworkTimeoutError: connection timed out

代码适用于小文件夹结构,但代码不适用于更多文件夹结构。我尝试了更多方法,但我没有找到更多文件夹结构的工作代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。