如何修复“ MongoError:数据库名称不能包含字符'/'”

时间:2019-04-29 02:57:06

标签: node.js mongodb express mongoose graphql

我是MongoDB和GraphQL的新手。我试图研究本教程https://www.djamware.com/post/5c75d68880aca754f7a9d1ed/node-express-angular-7-graphql-and-mongodb-crud-web-app,并陷入第二步(MongoDB连接)。这是错误: Error1 Error2

这是我的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;

如何解决该错误?我真的很困

1 个答案:

答案 0 :(得分:2)

感谢您的帮助。 我错过了访问数据库的特权。当我跑步 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时,连接成功了