1

I'm new in MongoDB and GraphQL. I tried to study this tutorial https://www.djamware.com/post/5c75d68880aca754f7a9d1ed/node-express-angular-7-graphql-and-mongodb-crud-web-app and get stuck in 2nd step (MongoDB connection). Here's the error : Error1 Error2

This is my 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;

How to fix that error? I'm really stuck

Ulfah Putri
  • 41
  • 2
  • 12
  • connection uri doesn't look right, if you haven't changed mongodb port try `mongodb://localhost:27107/node-graphql` – 1565986223 Apr 29 '19 at 03:12
  • @1556089774 I put the node-graphl folder inside the GraphQL folder. Is that allowed? I've tried your suggestion and this is what I got `{ 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 }` – Ulfah Putri Apr 29 '19 at 03:18
  • It's not about the folder layout, `node-graphql` here refers to db name https://docs.mongodb.com/manual/reference/connection-string/ – 1565986223 Apr 29 '19 at 03:21
  • on your command line type `mongo` and see if you can access, otherwise your mongodb server is not running – 1565986223 Apr 29 '19 at 03:22
  • @1556089774 i've opened mongo in my command line `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` and still I get this eror (`{ 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 }` ) when I run "npm start" in command line – Ulfah Putri Apr 29 '19 at 03:58

1 Answers1

2

Thanks for the help. I miss the privilege for accessing the database. When I run mongod.exe --dbpath "C:\xampp\htdocs\GraphQL\node-graphql\mongodb\data" I get this error

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.

Then I try this solution MongoDB: Server has startup warnings ''Access control is not enabled for the database'' and the connection was successful when I start the npm again

Ulfah Putri
  • 41
  • 2
  • 12