如何修复“MongoError:数据库名称不能包含字符'/'”
Posted
技术标签:
【中文标题】如何修复“MongoError:数据库名称不能包含字符\'/\'”【英文标题】:How to fix "MongoError: database names cannot contain the character '/' "如何修复“MongoError:数据库名称不能包含字符'/'” 【发布时间】:2019-09-17 16:01:34 【问题描述】:我是 MongoDB 和 GraphQL 的新手。我尝试学习本教程https://www.djamware.com/post/5c75d68880aca754f7a9d1ed/node-express-angular-7-graphql-and-mongodb-crud-web-app 并陷入第二步(MongoDB 连接)。这是错误: Error1Error2
这是我的 app.js:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
//declare the Mongoose module
var mongoose = require('mongoose');
//Create a connection to the MongoDB server
mongoose.connect('mongodb://localhost/GraphQL/node-graphql', promiseLibrary: require('bluebird'), useNewUrlParser: true )
.then(() => console.log('connection successful'))
.catch((err) => console.error(err));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded( extended: false ));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next)
next(createError(404));
);
// error handler
app.use(function(err, req, res, next)
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : ;
// render the error page
res.status(err.status || 500);
res.render('error');
);
module.exports = app;
如何解决该错误?我真的卡住了
【问题讨论】:
连接 uri 看起来不正确,如果您没有更改 mongodb 端口,请尝试mongodb://localhost:27107/node-graphql
@1556089774 我将 node-graphl 文件夹放在 GraphQL 文件夹中。这是允许的吗?我已经尝试了你的建议,这就是我得到的 Error: connect ECONNREFUSED 127.0.0.1:27107 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1104:14) errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 27107
不是文件夹布局的问题,node-graphql
这里指的是db名称docs.mongodb.com/manual/reference/connection-string
在你的命令行输入mongo
看看你是否可以访问,否则你的mongodb服务器没有运行
@1556089774 我已经在命令行MongoDB shell version v4.0.9 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session "id" : UUID("b073fff9-051f-47a3-889d-48a505b35eae") MongoDB server version: 4.0.9
中打开了mongo,但当我在命令行中运行“npm start”时仍然出现此错误( Error: connect ECONNREFUSED 127.0.0.1:27107 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1104:14) errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 27107
)
【参考方案1】:
感谢您的帮助。
我错过了访问数据库的特权。当我跑
mongod.exe --dbpath "C:\xampp\htdocs\GraphQL\node-graphql\mongodb\data"
我收到此错误
2019-04-29T10:37:08.554+0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-29T10:37:08.557+0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
然后我尝试这个解决方案MongoDB: Server has startup warnings ''Access control is not enabled for the database'',当我再次启动npm时连接成功
【讨论】:
以上是关于如何修复“MongoError:数据库名称不能包含字符'/'”的主要内容,如果未能解决你的问题,请参考以下文章