0

I'm having a play with the MEAN stack in order to teach myself a bit of javascript, and use Angular, and i'm having trouble setting up a MEAN environment.

I'm trying to connect to my Mongodb database, and read the contents of the "users" collection, which should contain two records, "John Doe", and "Jane Doe".

This is what my connection string looks like, as well as the constants I have at the top of the api.js

//Importing express, Router, Mongoclient and ObjectID
const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;

//Connect
const connection = (closure) => {
    //Connecting to "mongod://localhost" line, "/mean" is the name of the DB you
    //want to connect too
    return MongoClient.connect('mongodb://localhost:27017/mean', { useNewUrlParser: true }, (err, client) => { 
        if (err) return console.log(err);

        var db = client.db('mean');

        db.collection('users').findOne({}, function (findErr, result) {
            if (findErr) throw findErr;
            console.log(result.name);
            client.close()
        });

        //closure(db);
    });
};

When I run "node server" from the command line, it runs ok with no error:

C:\Users\jack_\OneDrive\Documents\code\mean>node server
Running on localhost:3000

However, when I try and navigate to "localhost:3000/api/users", nothing happens, the page gets stuck loading, and if I go back to the command line, it shows the first entry in "users":

C:\Users\jack_\OneDrive\Documents\code\mean>node server
Running on localhost:3000
John Doe

I can open the "localhost:3000/api" with no problem, and it takes me to the AngularJS landing page just fine.

I've had a read of over stackoverflow questions and found that there may be a problem with the mongodb driver, as they accidently released a beta version onto npm, so i checked my database version and the driver version:

C:\Users\jack_\OneDrive\Documents\code\mean>"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --version
db version v4.0.2
git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
allocator: tcmalloc
modules: none
build environment:
    distmod: 2008plus-ssl
    distarch: x86_64
    target_arch: x86_64

Database version:

C:\Users\jack_\OneDrive\Documents\code\mean>"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe" --version
MongoDB shell version v4.0.2
git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
allocator: tcmalloc
modules: none
build environment:
    distmod: 2008plus-ssl
    distarch: x86_64
    target_arch: x86_64

Looking at the other stackoverflow answers, because of the version I'm using, MongoClient.connect now returns a client object containing the database object

So I ammended the code with:

MongoClient.connect('mongodb://localhost:27017/mean', { useNewUrlParser: true }

and

var db = client.db('mean');

        db.collection('users').findOne({}, function (findErr, result) {
            if (findErr) throw findErr;
            console.log(result.name);
            client.close()

This got rid of my original error, which was "db.collections is not a function", but, according to the tutorial I'm following, I should see the contents of the "users" collection in a web browser, along with some other information, kinda like this:

{"status":200,"data":[{"_id":"598ce66bf7d6d70def3d9f6f","name":"John Doe"}

But, I'm not, So what have I done wrong? or am I missing something, bearing in mind I was writing this at 3AM, so I more than likely missed something

Thanks in Advance

1 Answers1

0

You can use below code to connect with db and find the data

const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;

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

MongoClient.connect(url, function(err, client) {
  if (err) throw err;
  var db = client.db("mean");
  db.collection("users").findOne({}, function(err, result) {
    if (err) throw err;
    console.log(result.name);
    db.close();
  });
});
  • When I add your code to file, I get a different error now: var MongoClient = require('mongodb').MongoClient; ^ SyntaxError: Identifier 'MongoClient' has already been declared and when i try commenting out either "var MongoClient", or "const MongoClient", I get an error saying it isn't declared – LegitShellAccount Sep 13 '18 at 17:22
  • ok i got it you can remove this line " var MongoClient = require('mongodb').MongoClient; " from the code any try again it will work – Sagar Pandey Sep 14 '18 at 08:23
  • Thanks, that fixed that problem, but now I'm getting a different error: if (err) throw err; ^ Error: Invalid schema, expected `mongodb` or `mongodb+srv` Most of the other questions I've found with the error says it's because of my url I'm using to connect is wrong, and they say the fix is adding "mongod://" to the start of it, which I have done, so the string is now: "var url = mongod://localhost:27017/mean" Any more ideas? – LegitShellAccount Sep 17 '18 at 22:08