【问题标题】:Cannot read property 'db' of null javascript with parcel无法使用包裹读取 null javascript 的属性“db”
【发布时间】:2019-06-27 15:15:44
【问题描述】:

我尝试使用我的 openlayers 地图设置 mongodb 系统,但它不起作用:未捕获的类型错误:无法读取 null 的属性“db”。我关于 mongodb 的部分代码是:

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";
    MongoClient.connect(url, function(err, db) {
            var tapDB = db.db("tapDB"); //<-- here is the error


})

我想这个错误可能是因为我使用的是节点 server.js 的 npm start instear,但我不确定,因为我是新手。 Mongodb 通过 cmd 通过执行以下命令启动:“mongod”,然后在另一个 cmd 上运行 mongo。

更新:对于和我有同样问题的每个人,我建议删除包裹。这就是我所做的,现在它工作正常

【问题讨论】:

  • 退出 err 看看它说了什么。
  • 在做任何事情之前你应该检查err是否为空。如果它不为空,则发生错误。您可以记录err的内容,它将帮助您了解正在发生的事情。
  • 你应该检查err

标签: node.js mongodb npm openlayers parceljs


【解决方案1】:

我认为您当前在错误的位置提供了url - 您需要在调用.connect 之前提供MongoClient 的URL。根据 MongoDB 的 Node.js 驱动程序文档,它看起来应该是这样的:

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://localhost:27017';
const dbName = 'tapDB';
const client = new MongoClient(url);

client.connect(function(err) {
  console.log("Connected successfully to server");
  const db = client.db(dbName);

  // use database connection here

  client.close();
});

在这里查看文档:http://mongodb.github.io/node-mongodb-native/3.2/tutorials/connect/

更新:

您也可以使用 ES6 async/await 执行上述操作,从长远来看,它比回调或原生 Promise 更易于使用,这是我们的设置:

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'tapDB';

(async () => { // async/await function that will run immediately

  let client;
  try {
    client = await MongoClient.connect(url);
  } catch (err) { throw err; }
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  let res;
  try {
    res = await db.collection("markers").insertMany([{ test1: true, test2: "3/5" }]);
  } catch (err) { throw err; }

  try {
    await client.close();
  } catch (err) { throw err; }
});

【讨论】:

  • 感谢@MatthewP 的更新。顺便说一句,我无法回复评论,因为我的帖子字符太多。我试过你的代码,但我收到了这个错误:Uncaught ReferenceError: regeneratorRuntime is not defined at HTMLDivElement.
  • 我搜索了一下,我注意到这个错误来自我安装的依赖项,我会尝试修复它
【解决方案2】:

使用 Javascript Promises ES6 代码更清晰

看看我的代码

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

MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true }).then(client => {
    console.log('Connected to MongoDB server')

    const db = client.db('dbName')

    // Here you can place your operations with the bd

    client.close();
}, e => console.log('Error to connect', e))

希望对你有所帮助

祝你好运!

【讨论】:

  • 您好,我尝试了您的代码,但出现此错误:连接 MongoNetworkError: 首次连接时无法连接到服务器 [localhost:27017] [TypeError: net.createConnection is不是函数] 我搜索了一下,如果我的数据库没有运行,看起来这个错误会发生,但是我在我的 cmd 上做了 mongod 并且它正在运行
  • 那个错误是因为mongodb没有运行。通过控制台运行 mongod --dbpath directoryForDatafiles - 默认为 \data \db\ 并检查此日志:MongoDB starting: pid = 15912 port = 27017 pid 可以是另一个
  • 我在 cmd 中输入了这个(截图:noelshack.com/2019-26-4-1561661117-capture.png),我可以看到 MongoDB 开始的日志:在我的情况下是 pid=14164 port=27017 但它仍然无法正常工作
  • 请在新的 cmd 中:telnet localhost 27017。在你的 package.json 检查你的项目的依赖项是否是 mongodb
  • 仍然无法使用 telnet localhost 27017 并且我的 mongodb 版本是 3.2.7 。我认为它不起作用,因为我正在使用包裹来构建我的应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 2013-03-21
  • 2019-08-05
相关资源
最近更新 更多