所以,我尝试使用MongoDB数据库启动我的Express服务器,但是我遇到了下一个错误的问题:
MongoError:请求了多数读取问题,但服务器未启动--enableMajorityReadConcern。
我的server.js:
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const MongoClient = require('mongodb').MongoClient;
const mongoURL = "mongodb://myDB";
let dbArr = {};
MongoClient.connect(mongoURL, (err, client) => {
let db = client.db('test_db');
let changeStream = db.collection('employers').watch();
// change_strems.on('change', function(change){
// console.log(JSON.stringify(change));
// });
changeStream.on('change', function(change) {
expect(change).to.exist;
// Since changeStream has an implicit seession,
// we need to close the changeStream for unit testing purposes
changeStream.close();
client.close();
done();
});
db.collection('employers').find().toArray((err, results) => {
dbArr = results;
// console.log(results)
});
});
app.get('/users', function(req, res,next) {
res.send(dbArr);
});
io.on('connection', function(client) {
client.on('form_message', function(data) {
db.collection('employers').save(data, (err, result) => {
if (err) return console.log(err)
});
// db.collection('employers').find(data, (err, data) => {
// if (err) return console.log(err)
// if (data) {
// client.emit('click', data);
// }
// });
console.log('New Message', data);
});
});
server.listen(8000);