I am trying to run a Node.js server with MongoDB. I followed the tutorials closely. Here is my app.js file:
const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const app = express(); const MongoClient = require('mongodb').MongoClient; const asser = require('assert'); app.use(bodyParser.urlencoded({ extended: true })); var url = 'mongodb://localhost:27017/test'; var db; app.listen(80, () => { console.log('listening on 80') }) MongoClient.connect(url, (err, database) => { if (err) return console.log(err) db = database }) app.post('/register', function(req, res) { console.log("asdf"); res.send('1'); port.write(JSON.stringify(req.body) + "n"); }); function onOpen() { console.log("OPENED"); } function onData(data) { console.log(data); }
` When I try to run it, however, I get the following error:
pi@raspberrypi:~/Server $ sudo node app.js listening on 80 { MongoError: unsupported server version at /home/pi/Server/node_modules/mongodb- core/lib/topologies/server.js:367:39 at /home/pi/Server/node_modules/mongodb- core/lib/connection/pool.js:542:18 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9) name: 'MongoError', message: 'unsupported server version' }
How can I fix this? My package.json file says I am using version ^3.0.0-rc0. I cant seem to find a solution anywhere online.
Advertisement
Answer
Upgrade to the latest version of MongoDB. The database system, not the driver that is listed in package.json.
You also have to update your connection code slightly:
MongoClient.connect(url, (err, database) => { if (err) return console.log(err) db = database })
should be
MongoClient.connect(url, (err, client) => { if (err) return console.log(err) db = client.db('test') })
That’s because the syntax changed somewhat for version 3.0.0 of the mongodb driver.