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