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

时间:2021-07-02 05:55:37

标签: node.js database mongodb directory-structure chokidar

我正在使用 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, function(err, 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, function(err, 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

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

0 个答案:

没有答案