Failing to connect to MongoDB hosted on mlab












0















Background



Making a small web app that connects to a Mongo DB hosted with Mlab. I've created the DB on mlab, and created users with read/write permission. I've also created a users collection with several records.



The Problem



When I try and connect to the database using the code on mongo.github.io, I hit the error:



/home/ed/dev/mongo-demo/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^

TypeError: Cannot read property 'db' of null


The Code



var MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434';

// Database Name
const dbName = 'princee3-music';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");

const db = client.db(dbName);

client.close();
});


What I Have Tried



Oddly, if I connect through the shell using:



mongo ds115434.mlab.com:15434/princee3-music -u <dbuser> -p <dbpassword>


That works fine, or if I wrap the connection in an anonymous self-calling async function, it also connects.



Async Wrapper



const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434/';
const dbName = 'princee3-music';

(async() => {
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true});
const db = client.db(dbName);
db.collection('users').insertOne({
email: user.email,
pass: hashedPassword,
admin: true
}, (err, result) => {
if (err) {
reject({error: err});
} else {
resolve({message: 'okay'});
}
});
client.close();
})();


Any pointers on where I may be going wrong would be great.










share|improve this question























  • Can you please give me the output of console.log(err) to be placed above const db = client.db(dbName)?

    – Asten Mies
    Nov 25 '18 at 13:21













  • { MongoError: Authentication failed.

    – Ed Prince
    Nov 25 '18 at 13:23











  • Why are you not satisfied with async? It makes sense to wait for the connection to effectively occur before doing anything in there...

    – Asten Mies
    Nov 25 '18 at 13:29
















0















Background



Making a small web app that connects to a Mongo DB hosted with Mlab. I've created the DB on mlab, and created users with read/write permission. I've also created a users collection with several records.



The Problem



When I try and connect to the database using the code on mongo.github.io, I hit the error:



/home/ed/dev/mongo-demo/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^

TypeError: Cannot read property 'db' of null


The Code



var MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434';

// Database Name
const dbName = 'princee3-music';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");

const db = client.db(dbName);

client.close();
});


What I Have Tried



Oddly, if I connect through the shell using:



mongo ds115434.mlab.com:15434/princee3-music -u <dbuser> -p <dbpassword>


That works fine, or if I wrap the connection in an anonymous self-calling async function, it also connects.



Async Wrapper



const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434/';
const dbName = 'princee3-music';

(async() => {
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true});
const db = client.db(dbName);
db.collection('users').insertOne({
email: user.email,
pass: hashedPassword,
admin: true
}, (err, result) => {
if (err) {
reject({error: err});
} else {
resolve({message: 'okay'});
}
});
client.close();
})();


Any pointers on where I may be going wrong would be great.










share|improve this question























  • Can you please give me the output of console.log(err) to be placed above const db = client.db(dbName)?

    – Asten Mies
    Nov 25 '18 at 13:21













  • { MongoError: Authentication failed.

    – Ed Prince
    Nov 25 '18 at 13:23











  • Why are you not satisfied with async? It makes sense to wait for the connection to effectively occur before doing anything in there...

    – Asten Mies
    Nov 25 '18 at 13:29














0












0








0








Background



Making a small web app that connects to a Mongo DB hosted with Mlab. I've created the DB on mlab, and created users with read/write permission. I've also created a users collection with several records.



The Problem



When I try and connect to the database using the code on mongo.github.io, I hit the error:



/home/ed/dev/mongo-demo/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^

TypeError: Cannot read property 'db' of null


The Code



var MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434';

// Database Name
const dbName = 'princee3-music';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");

const db = client.db(dbName);

client.close();
});


What I Have Tried



Oddly, if I connect through the shell using:



mongo ds115434.mlab.com:15434/princee3-music -u <dbuser> -p <dbpassword>


That works fine, or if I wrap the connection in an anonymous self-calling async function, it also connects.



Async Wrapper



const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434/';
const dbName = 'princee3-music';

(async() => {
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true});
const db = client.db(dbName);
db.collection('users').insertOne({
email: user.email,
pass: hashedPassword,
admin: true
}, (err, result) => {
if (err) {
reject({error: err});
} else {
resolve({message: 'okay'});
}
});
client.close();
})();


Any pointers on where I may be going wrong would be great.










share|improve this question














Background



Making a small web app that connects to a Mongo DB hosted with Mlab. I've created the DB on mlab, and created users with read/write permission. I've also created a users collection with several records.



The Problem



When I try and connect to the database using the code on mongo.github.io, I hit the error:



/home/ed/dev/mongo-demo/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^

TypeError: Cannot read property 'db' of null


The Code



var MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434';

// Database Name
const dbName = 'princee3-music';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");

const db = client.db(dbName);

client.close();
});


What I Have Tried



Oddly, if I connect through the shell using:



mongo ds115434.mlab.com:15434/princee3-music -u <dbuser> -p <dbpassword>


That works fine, or if I wrap the connection in an anonymous self-calling async function, it also connects.



Async Wrapper



const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://<user>:<pass>@ds115434.mlab.com:15434/';
const dbName = 'princee3-music';

(async() => {
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true});
const db = client.db(dbName);
db.collection('users').insertOne({
email: user.email,
pass: hashedPassword,
admin: true
}, (err, result) => {
if (err) {
reject({error: err});
} else {
resolve({message: 'okay'});
}
});
client.close();
})();


Any pointers on where I may be going wrong would be great.







javascript node.js database mongodb mlab






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 13:13









Ed PrinceEd Prince

3471520




3471520













  • Can you please give me the output of console.log(err) to be placed above const db = client.db(dbName)?

    – Asten Mies
    Nov 25 '18 at 13:21













  • { MongoError: Authentication failed.

    – Ed Prince
    Nov 25 '18 at 13:23











  • Why are you not satisfied with async? It makes sense to wait for the connection to effectively occur before doing anything in there...

    – Asten Mies
    Nov 25 '18 at 13:29



















  • Can you please give me the output of console.log(err) to be placed above const db = client.db(dbName)?

    – Asten Mies
    Nov 25 '18 at 13:21













  • { MongoError: Authentication failed.

    – Ed Prince
    Nov 25 '18 at 13:23











  • Why are you not satisfied with async? It makes sense to wait for the connection to effectively occur before doing anything in there...

    – Asten Mies
    Nov 25 '18 at 13:29

















Can you please give me the output of console.log(err) to be placed above const db = client.db(dbName)?

– Asten Mies
Nov 25 '18 at 13:21







Can you please give me the output of console.log(err) to be placed above const db = client.db(dbName)?

– Asten Mies
Nov 25 '18 at 13:21















{ MongoError: Authentication failed.

– Ed Prince
Nov 25 '18 at 13:23





{ MongoError: Authentication failed.

– Ed Prince
Nov 25 '18 at 13:23













Why are you not satisfied with async? It makes sense to wait for the connection to effectively occur before doing anything in there...

– Asten Mies
Nov 25 '18 at 13:29





Why are you not satisfied with async? It makes sense to wait for the connection to effectively occur before doing anything in there...

– Asten Mies
Nov 25 '18 at 13:29












1 Answer
1






active

oldest

votes


















1














The official mLab docs advise to connect like below. It has to be asynchronous , in order to wait for the connection to occur, or the client will be null, thus throwing an error saying that it can’t read property db of null.



On the other hand, you async has useNewUrlParser which might be the key to have a successful connection, see this issue



MongoClient.connect(url, { useNewUrlParser: true }).then(client => client.db())





share|improve this answer


























  • Still getting an authentication error when trying this way

    – Ed Prince
    Nov 25 '18 at 13:40











  • @EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

    – Asten Mies
    Nov 25 '18 at 13:47













  • Tried, and still the same authentication failed

    – Ed Prince
    Nov 25 '18 at 13:53











  • @EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

    – Asten Mies
    Nov 25 '18 at 14:02











  • So it turns out you have to add the db name to the end of the URI, I hadn't realised that

    – Ed Prince
    Nov 25 '18 at 14:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467825%2ffailing-to-connect-to-mongodb-hosted-on-mlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The official mLab docs advise to connect like below. It has to be asynchronous , in order to wait for the connection to occur, or the client will be null, thus throwing an error saying that it can’t read property db of null.



On the other hand, you async has useNewUrlParser which might be the key to have a successful connection, see this issue



MongoClient.connect(url, { useNewUrlParser: true }).then(client => client.db())





share|improve this answer


























  • Still getting an authentication error when trying this way

    – Ed Prince
    Nov 25 '18 at 13:40











  • @EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

    – Asten Mies
    Nov 25 '18 at 13:47













  • Tried, and still the same authentication failed

    – Ed Prince
    Nov 25 '18 at 13:53











  • @EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

    – Asten Mies
    Nov 25 '18 at 14:02











  • So it turns out you have to add the db name to the end of the URI, I hadn't realised that

    – Ed Prince
    Nov 25 '18 at 14:03
















1














The official mLab docs advise to connect like below. It has to be asynchronous , in order to wait for the connection to occur, or the client will be null, thus throwing an error saying that it can’t read property db of null.



On the other hand, you async has useNewUrlParser which might be the key to have a successful connection, see this issue



MongoClient.connect(url, { useNewUrlParser: true }).then(client => client.db())





share|improve this answer


























  • Still getting an authentication error when trying this way

    – Ed Prince
    Nov 25 '18 at 13:40











  • @EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

    – Asten Mies
    Nov 25 '18 at 13:47













  • Tried, and still the same authentication failed

    – Ed Prince
    Nov 25 '18 at 13:53











  • @EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

    – Asten Mies
    Nov 25 '18 at 14:02











  • So it turns out you have to add the db name to the end of the URI, I hadn't realised that

    – Ed Prince
    Nov 25 '18 at 14:03














1












1








1







The official mLab docs advise to connect like below. It has to be asynchronous , in order to wait for the connection to occur, or the client will be null, thus throwing an error saying that it can’t read property db of null.



On the other hand, you async has useNewUrlParser which might be the key to have a successful connection, see this issue



MongoClient.connect(url, { useNewUrlParser: true }).then(client => client.db())





share|improve this answer















The official mLab docs advise to connect like below. It has to be asynchronous , in order to wait for the connection to occur, or the client will be null, thus throwing an error saying that it can’t read property db of null.



On the other hand, you async has useNewUrlParser which might be the key to have a successful connection, see this issue



MongoClient.connect(url, { useNewUrlParser: true }).then(client => client.db())






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 13:50

























answered Nov 25 '18 at 13:37









Asten MiesAsten Mies

543210




543210













  • Still getting an authentication error when trying this way

    – Ed Prince
    Nov 25 '18 at 13:40











  • @EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

    – Asten Mies
    Nov 25 '18 at 13:47













  • Tried, and still the same authentication failed

    – Ed Prince
    Nov 25 '18 at 13:53











  • @EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

    – Asten Mies
    Nov 25 '18 at 14:02











  • So it turns out you have to add the db name to the end of the URI, I hadn't realised that

    – Ed Prince
    Nov 25 '18 at 14:03



















  • Still getting an authentication error when trying this way

    – Ed Prince
    Nov 25 '18 at 13:40











  • @EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

    – Asten Mies
    Nov 25 '18 at 13:47













  • Tried, and still the same authentication failed

    – Ed Prince
    Nov 25 '18 at 13:53











  • @EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

    – Asten Mies
    Nov 25 '18 at 14:02











  • So it turns out you have to add the db name to the end of the URI, I hadn't realised that

    – Ed Prince
    Nov 25 '18 at 14:03

















Still getting an authentication error when trying this way

– Ed Prince
Nov 25 '18 at 13:40





Still getting an authentication error when trying this way

– Ed Prince
Nov 25 '18 at 13:40













@EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

– Asten Mies
Nov 25 '18 at 13:47







@EdPrince I have edited the answer, you may need useNewUrlParser depending on the version

– Asten Mies
Nov 25 '18 at 13:47















Tried, and still the same authentication failed

– Ed Prince
Nov 25 '18 at 13:53





Tried, and still the same authentication failed

– Ed Prince
Nov 25 '18 at 13:53













@EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

– Asten Mies
Nov 25 '18 at 14:02





@EdPrince if you guarantee that your async wrapper works then the only remaining difference is the trailing slash at the end of your mongoUrl. For the rest I can’t help any further sorry

– Asten Mies
Nov 25 '18 at 14:02













So it turns out you have to add the db name to the end of the URI, I hadn't realised that

– Ed Prince
Nov 25 '18 at 14:03





So it turns out you have to add the db name to the end of the URI, I hadn't realised that

– Ed Prince
Nov 25 '18 at 14:03




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467825%2ffailing-to-connect-to-mongodb-hosted-on-mlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'