簡體   English   中英

升級Mongo后Nodejs和mongodb連接錯誤

[英]Nodejs and mongodb connection error after upgrading Mongo

我正在嘗試使用Express和mongodb構建一些Todo應用程序,但是我在nodejs和mongo連接方面遇到了一些問題,我在互聯網上進行了搜索,但沒有得到解決我的問題的方法。

當嘗試按以下代碼從節點js連接到mongodb時:

// This to require Express framework to use it
const express = require('express');
const csrf = require('csurf');
const errorhandler = require('errorhandler')
// Loads the piece of middleware for sessions
const logger = require('morgan');
const favicon = require('serve-favicon');
const methodOverride = require('method-override');
// This is a middleware to handle requests and parse the forms
const bodyParser = require('body-parser');
const routes = require('./routes');

const tasks = require('./routes/tasks');
const http = require('http');
const path = require('path');
const mongoskin = require('mongoskin');
const cookieParser = require('cookie-parser');
const session = require('express-session');

const db = mongoskin.db("mongodb://localhost:27017/todo");

// Define object of express
const app = express();

app.use((req, res, next) => {
    req.db = {};
    req.db.tasks = db.collection('tasks');
    next();
});

// This makes us can access app name from any JADe template
app.locals.appname = "Todo App Express.js";
app.locals.moment = require('moment');

// set app port to 8081 port
app.set('port', 8081);
// these tells express where's project safe templates in, and tell what's
//  their extension will be.
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// show express favicon in browser
app.use(favicon(path.join('public', 'favicon.ico')));
// this will print requests in terminal
app.use(logger('dev'));
// using body parser to can parse requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride());
// to use CSRF we need cookie parser
app.use(cookieParser('CEAF3FA4-F385-49AA-8FE4-54766A9874F1'));
app.use(session({
  secret: '59B93087-78BC-4EB9-993A-A61FC844F6C9',
  resave: true,
  saveUninitialized: true
}));
app.use( csrf() );

// to process LESS stylesheets into CSS ones
app.use(require('less-middleware')(path.join(__dirname, 'public')));
// to use other static files which inside public
app.use(express.static(path.join(__dirname, '/public')));
// to make csrf used in templates
app.use( (req, res, next) => {
  res.locals._csrf = req.csrfToken();
  return next();
});

app.param('task_id', (req, res, next, taskId) => {
    req.db.tasks.findById(taskId, (error, task) => {
      if (error) return next(error);
      if (!task) return next(new Error('Task is not found.'));

      req.task = task;
      return next();
    });
});

// homepage to show our todo homepage
app.get('/', routes.index);
// list all created tasks [in-progress ones]
app.get('/tasks', tasks.list);
// this to mark all task as completed in one step
app.post('/tasks', tasks.markAllcompleted);
// this to add new task
app.post('/tasks', tasks.add);
// this to mark specfic task as completed
app.post('/tasks/:task_id', tasks.markcompleted);
// this to delete specfic task by :task_id
app.delete('/tasks/:task_id', tasks.del);
// this to list all completed tasks
app.get('/tasks/completed', tasks.completed);
// this 404 error page to show it if user requested any route doesn't exist in our routes
app.all('*', (req, res) => {
  res.status(404).send();
});

if ('development' == app.get('env')) {
  app.use(errorhandler());
}


// create our server
http.createServer(app).listen(app.get('port'),
  () => {
    console.log('Express server listening on port ' + app.get('port'));
  });

我在終端中遇到此錯誤:

/home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongodb/lib/mongo_client.js:804
          throw err;
          ^

TypeError: Cannot read property 'apply' of undefined
    at EventEmitter.<anonymous> (/home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongoskin/lib/collection.js:51:21)
    at Object.onceWrapper (events.js:272:13)
    at EventEmitter.emit (events.js:180:13)
    at /home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongoskin/lib/utils.js:134:27
    at result (/home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongodb/lib/utils.js:414:17)
    at executeCallback (/home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongodb/lib/utils.js:406:9)
    at /home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongodb/lib/mongo_client.js:271:5
    at connectCallback (/home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongodb/lib/mongo_client.js:940:5)
    at /home/luffy/nodejs-train/todo-express-mongodb/node_modules/mongodb/lib/mongo_client.js:801:11
    at process._tickCallback (internal/process/next_tick.js:112:11)
[nodemon] app crashed - waiting for file changes before starting...

在我的Firefox中:

Unable to connect

Firefox can’t establish a connection to the server at localhost:8081.

使用NodeJS V9.8.0和mongodb v3.4.14

即使我們遇到了這個問題,似乎也與后來的“ mongoskin”版本有關。 為什么不嘗試使用mongodb

節省時間和更好的選擇!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM