mongoose 连接警告报错信息处理 { useNewUrlParser: true } 、{ useUnifiedTopology: true }

整个章节呢,记录一个错误处理

1.、在windows安装好mongodb后,启动没问题,创建数据库没问题;但是在node环境使用mongoose之后,有一些警告报信息,附上我的连接代码:

//...
mongoose.connect('mongodb://127.0.0.1:27017/');
mongoose.connection.once('open',() => {
  console.log('connceted to database.')
});
//...

2、然后,控制台输出打印信息:

[nodemon] starting `node server.js`
(node:35104) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:35104) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
start server in port 4000
connceted to database.

3、这里显示已经连上mongodb了,可能是好久没用mongodb了,出现了两个警告,所以呢,对于我这种代码强迫症来说,就需要解决这个警告信息,按照提示我们加上对应的参数如下:

mongoose.connect('mongodb://127.0.0.1:27017/', { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connection.once('open',() => {
  console.log('connceted to database.')
});

4、好吧,接下来继续报错,如下:

throw MongoError.create({ message: 'database name must be a string', driver: true });
    ^

MongoError: database name must be a string
    at Function.create (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\core\error.js:44:12)
    at validateDatabaseName (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\operations\db_ops.js:549:22)
    at new Db (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\db.js:190:3)
    at MongoClient.db (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\mongo_client.js:275:14)
    at E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongoose\lib\connection.js:637:62
    at result (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\operations\execute_operation.js:75:17)
    at executeCallback (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\operations\execute_operation.js:68:9)
    at err (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\operations\connect.js:193:7)
    at connectCallback (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\operations\connect.js:340:5)
    at topology.connect (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\operations\connect.js:472:7)
    at Server.connectHandler (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\core\sdam\topology.js:259:45)
    at Object.onceWrapper (events.js:286:20)
    at Server.emit (events.js:203:15)
    at Pool.<anonymous> (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\core\sdam\server.js:470:12)
    at Pool.emit (events.js:198:13)
    at connect (E:\web\github-riverscoder\graphQL-examples\node_modules\[email protected]@mongodb\lib\core\connection\pool.js:624:10)

5、说数据库名必须是一个字符串,然后仔细检查了下,发现没有带上数据库名,好,接下来,我们带上我们的数据库名称damiao-test

mongoose.connect('mongodb://127.0.0.1:27017/damiao-test', { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connection.once('open',() => {
  console.log('connceted to database.')
});

6、控制台打印如下:

[nodemon] starting `node server.js`
start server in port 4000
connceted to database.

7、好的 ,大功告成 !!

你可能感兴趣的:(mongoose,mongodb,node,node,mongodb)