0

I tried to set up a mongodb system with my openlayers map, but it is not working : Uncaught TypeError: Cannot read property 'db' of null. My part of code about mongodb is :

    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


})

I suppose that this error is maybe because i am using npm start instear of node server.js, but I am not sure because i am a newbie. Mongodb is started via the cmd by doing the following command :"mongod" and then mongo on an other cmd.

UPDATE: For everyone having the same problem than me, i'd recommand deleting parcel. That's what I did and now it works fine

Leftium
  • 16,497
  • 6
  • 64
  • 99
Falseee
  • 79
  • 10

2 Answers2

2

I believe you are currently providing the url in the wrong place - you need to provide the URL to MongoClient before calling .connect. As per MongoDB's Node.js Driver documentation it should look think this:

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();
});

Have a look a the documentation here: http://mongodb.github.io/node-mongodb-native/3.2/tutorials/connect/

UPDATE:

You can also do the above using ES6 async/await which is in the long run simpler to use than a callback or native promises, this is our setup:

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; }
});
Matthew P
  • 717
  • 1
  • 7
  • 22
  • thanks for the update @MatthewP . By the way, i couldnt respond by a comment because my post had too much chars. I tried your code, but i am getting this error : **Uncaught ReferenceError: regeneratorRuntime is not defined at HTMLDivElement.** – Falseee Jun 27 '19 at 17:18
  • i searched a bit and i noticed that this error comes from a dependancies that i installed, i will try to fix that – Falseee Jun 27 '19 at 17:38
0

Using Javascript Promises ES6 is code is clearer

Look my code

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))

I hope I've helped

Good luck!

dpokey
  • 64
  • 6
  • Hello, I tried your code, but I am getting this error: **Error to connect MongoNetworkError: failed to connect to server [localhost:27017] on first connect [TypeError: net.createConnection is not a function]** I searched a bit and it looks like this errors happends if my db is not running, but i did mongod on my cmd and it is running – Falseee Jun 27 '19 at 17:23
  • That error is because mongodb is not running. By console run `mongod --dbpath directoryForDatafiles` - defaults to \data \db\ and check this logs: `MongoDB starting: pid = 15912 port = 27017` the pid can be another – dpokey Jun 27 '19 at 18:22
  • I typed this in the cmd (screenshot : https://www.noelshack.com/2019-26-4-1561661117-capture.png) and i can see the log with MongoDB starting : pid=14164 port=27017 in my case But it is still not working – Falseee Jun 27 '19 at 18:45
  • Please in a new cmd: `telnet localhost 27017`. And in your **package.json** check if in your dependencies of your project is the mongodb – dpokey Jun 27 '19 at 19:19
  • Still dont work with telnet localhost 27017 and my mongodb version is 3.2.7 . I think it doesnt work because i am using parcel to build my app. – Falseee Jun 27 '19 at 19:49
  • Hello. I did it, i just deleted something called "parcel" to build my app and it worked :) – Falseee Jul 01 '19 at 20:52