Node JS / Mongo DB:var queryStringStart = uri.indexOf('?');

时间:2018-02-08 18:23:07

标签: node.js mongodb

我一直在尝试连接到我的Mongo数据库,但是我收到了这个错误。

  

node_modules \ mongoose \ lib \ index.js:132 var queryStringStart =   uri.indexOf('&#39);

我不明白我在这里做错了什么。这是我的server.js

 ======================================================================
    var express = require('express');
    var path = require('path');
    var logger = require('morgan');
    var bodyParser = require('body-parser');
    var app = express();
    var server = require('http').Server(app);
    var mongoose = require('mongoose');                 // mongoose for mongodb
    var port = process.env.PORT || 8000;                // set the port
    var database = require('./config/database');            // load the database config
    var morgan = require('morgan');
    var methodOverride = require('method-override');
    var io = require('socket.io')(server);
    var messageId = {};

    // configuration ===============================================================
    //mongoose.connect(database.localUrl);  // Connect to local MongoDB instance. A remoteUrl is also available (modulus.io)

   HTTP-Method-Override header in the request
    // Connect to  Mongo DB
    app.use(bodyParser.urlencoded({extended:true}))
    app.use(bodyParser.json())
    mongoose.connect(database,{useMongoClient:true})
    mongoose.connection.on('error', function() {
        console.log('Could not connect to the database. Exiting now...');
        process.exit();
    });
    mongoose.connection.once('open', function() {
        console.log("Successfully connected to the database");
    })
    io.set('origins', '*:*');
    http = require('http'),
    server = http.createServer(function (req, res) {
        //res.writeHead(200,{'content-type':'text/plain'});
        //  res.write("Sever On");
        // res.end();
    }),
    io = io.listen(server);
    io.on('connection', function (socket) {
        console.log('User Connected -- Server Online');   
        socket.on('message', function (msg,msgId) {
            io.emit('message', "Hello");
            console.log("message from client:", msg);
             setInterval(function(){
              io.emit("messageStatus",msgId);
          },500)
       });
    });
    // app.get('/', function (req, res) {
    //     res.send('hello world')
    // })
    server.listen(port);
    console.log("App listening on port " + port);

我不确定导致错误的原因。请让我知道!

1 个答案:

答案 0 :(得分:0)

我最终想出来了:

此行错误

  mongoose.connect(database,{useMongoClient:true})

它应该是

  mongoose.connect(database.remoteUrl,{useMongoClient:true})

我有一个数据库。我已经列出了我的网址

module.exports = {
    remoteUrl : 'mongodb://username:password@admin@ec5-14-127-71-182.ap-south-2.compute.amazonaws.com:27017/testDB'        
};