在文件上使用带有require'mongodb'的browserify会导致TypeError:dns.resolveSrv不是函数

问题描述

我正在开发一个简单的chrome扩展程序,该扩展程序将允许我从URL到DB写入,获取删除URL。 我决定使用MongoDB,并在名为popup.js的JS文件中编写了函数。并且它们可以从IDE正常运行。

因为它将从我将browserify用作require('mongodb')的浏览器中运行。

我使用了browserify popup.js -o bundle.js 当运行bundle.js文件时,我得到TypeError: dns.resolveSrv is not a function(从ide和浏览器运行时) 尝试连接客户端时发生了这种情况。

我应该怎么做才能避免这个问题?

谢谢。

popup.js:

const {MongoClient} = require('mongodb');

async function checkURL(client,URL){
    const result = await client.db("url-uploader").collection("urls").findOne({URL: URL})
    return !(result===null)
}


async function uploadURL(){
    const uri = {myURI};
    const client = new MongoClient(uri,{useUnifiedTopology: true});
    try {
        await client.connect();
        let newURL = 'www.b.com'//tab.url
        if (!await checkURL(client,newURL)) {
            const result = await client.db("url-uploader").collection("urls").insertOne({URL: newURL})
        }
    } catch (e) {
        console.error(e);
    } finally {
        await client.close();
    }
}

async function removeURL(){
    const uri = {myURI};
    const client = new MongoClient(uri,{useUnifiedTopology: true});
    try {
        await client.connect();
        let oldURL = "www.blabla.com" //tab.url
        const result = await client.db("url-uploader").collection("urls").deleteOne({URL: oldURL})
    } catch (e) {
        console.error(e);
    } finally {
        await client.close();
    }
}

这是bundle.js中发生错误的部分:

_poll() {
    const generation = this.generation;
    dns.resolveSrv(this.srvAddress,(err,srvRecords) => {
      if (generation !== this.generation) {
        return;
      }

      if (err) {
        this.failure('DNS error',err);
        return;
      }

      const finalAddresses = [];
      srvRecords.forEach(record => {
        if (matchesParentDomain(record.name,this.srvHost)) {
          finalAddresses.push(record);
        } else {
          this.parentDomainMismatch(record);
        }
      });

      if (!finalAddresses.length) {
        this.failure('No valid addresses found at host');
        return;
      }

      this.success(finalAddresses);
    });
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)