无法从Node.js连接到MongoDB

时间:2018-11-08 09:10:32

标签: javascript node.js mongodb

我试图建立与MongoDB数据库的连接,但收到以下错误消息:

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

也许您会因为没有检查并重用以前的主题而对我产生怀疑。但是,尽管问题相似,但没有一个与我的情况相符。

对于连接,我正在关注本教程:http://mongodb.github.io/node-mongodb-native/3.1/quick-start/quick-start/(请看“连接到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();
});

曾经说过,有人可以帮我吗?我什么都找不到。我很困惑。

============== 更新(2018年11月8日-UTC + 1下午01:11)

如果我通过控制台运行脚本,则不会有任何问题。但是,我收到了弃用警告,要求我使用新的解析器。

因此,当我在更改后在Web浏览器上运行它时,错误消息如下:

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

是因为browserify吗?

1 个答案:

答案 0 :(得分:0)

从您的更新中得知,您尝试使用Browserify通过Web浏览器直接连接到MongoDB。

我认为这不会起作用。与MongoDB的连接是通过TCP连接完成的,更确切地说,这是您的浏览器将无法使用的:我非常怀疑这是否像使用Browserify那样简单,并且一切正常。在您的浏览器中,可能没有任何驱动程序可以连接到MongoDB,或者其他我不应该想到的其他先决条件。

请参见此以找到合适的答案:Connecting to mongodb through the browser?