简体   繁体   中英

Cannot connect to MongoDB from Node.js

I am trying to make a connection to my MongoDB database but I get the following error message:

AssertionError
actual: null
expected: RangeError: Maximum call stack size exceeded at isUndefined (<anonymous>:2748:21) at Object.exports.deprecate (<anonymous>:2330:7) at <anonymous>:2332:22 at <anonymous>:2332:41 at <anonymous>:2332:41 at <anonymous>:2332:41 at <anonymous>:2332:41 at <anonymous>:2332:41 at <anonymous>:2332:41 at <anonymous>:2332:41
generatedMessage: true
message: "null == [RangeError: Maximum call stack size exceeded]"
name: "AssertionError"
operator: "=="
stack: "AssertionError: null == [RangeError: Maximum call stack size exceeded]↵    at <anonymous>:73719:10↵    at err (<anonymous>:71483:14)↵    at executeCallback (<anonymous>:71472:25)↵    at executeOperation (<anonymous>:71490:7)↵    at MongoClient.connect (<anonymous>:64885:10)↵    at Object.274.assert (<anonymous>:73718:8)↵    at o (<anonymous>:1:265)↵    at r (<anonymous>:1:431)↵    at <anonymous>:1:460↵    at m (http://localhost/mudfeval-mobile/public/src/js/jquery-3.3.1.min.js:2:711)"
__proto__: Error
constructor: ƒ AssertionError(options)
__proto__: Object

Maybe you would reprobate me for not having checked and reused previous topics. But despite the similarity of the problem, none of them matches with my case.

For the connection, I am following this tutorial: http://mongodb.github.io/node-mongodb-native/3.1/quick-start/quick-start/ (look at "Connect to MongoDB"):

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

// Connection URL
const url = "mongodb://secretoes:thatstotallysecret@mudfeval-shard-00-00-uxnqk.gcp.mongodb.net:27017,mudfeval-shard-00-01-uxnqk.gcp.mongodb.net:27017,mudfeval-shard-00-02-uxnqk.gcp.mongodb.net:27017/test?ssl=true&replicaSet=mudfeval-shard-0&authSource=admin&retryWrites=true";;

// Database Name
const dbName = 'moodle';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

Once said that, could somebody help me? I cannot find out anything anywhere. I am puzzled.

============= UPDATE (11/8/2018 - 01:11 pm UTC+1)

If I run the script through console, I get no problem. However I receive a deprecation warning that asked me to use the new parser.

So, when I run it on web browser after those changes, the error message is the following:

VM188:1932 Uncaught 
AssertionError {name: "AssertionError", actual: null, expected: TypeError: qs.unescape is not a function
    at parseConnectionString (<anonymous>:52229:75)
    at …, operator: "==", message: "null == [TypeError: qs.unescape is not a function]", …}
actual: null
expected: TypeError: qs.unescape is not a function at parseConnectionString (<anonymous>:52229:75) at connect (<anonymous>:68112:3) at connectOp (<anonymous>:68216:3) at executeOperation (<anonymous>:71488:24) at MongoClient.connect (<anonymous>:64885:10) at Object.274.assert (<anonymous>:73718:8) at o (<anonymous>:1:265) at r (<anonymous>:1:431) at <anonymous>:1:460 at m (http://localhost/mudfeval-mobile/public/src/js/jquery-3.3.1.min.js:2:711)
generatedMessage: true
message: "null == [TypeError: qs.unescape is not a function]"
name: "AssertionError"
operator: "=="
stack: "AssertionError: null == [TypeError: qs.unescape is not a function]↵    at <anonymous>:73719:10↵    at err (<anonymous>:71483:14)↵    at executeCallback (<anonymous>:71472:25)↵    at executeOperation (<anonymous>:71490:7)↵    at MongoClient.connect (<anonymous>:64885:10)↵    at Object.274.assert (<anonymous>:73718:8)↵    at o (<anonymous>:1:265)↵    at r (<anonymous>:1:431)↵    at <anonymous>:1:460↵    at m (http://localhost/mudfeval-mobile/public/src/js/jquery-3.3.1.min.js:2:711)"
__proto__: Error

Is it because of browserify?

From your update I get that you try to connect directly to a MongoDB via web browser by using Browserify.

I do not think that this will work. A connection to MongoDB is done by a TCP connection, which will not be available from your browser, or to be more precise: I highly doubt that is that simple as using Browserify and everything is OK. There is probably no driver to connect to MongoDB in your browser or many other prerequisites which certainly are not all in my mind.

See this for a suitable answer: Connecting to mongodb through the browser?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM