TypeError: uri.indexOf is not a function

Question

So I'm new to JavaScript and I'm attempting to build a web application in Node.js. I'm want to add user authentication into my app with passport.js. But when I try to start the server on the command prompt, I keep receiving this error:

TypeError: uri.indexOf is not a function
at checkReplicaSetInUri (C:\spectray\node_modules\mongoose\lib\index.js:115:30)
at Mongoose.connect (C:\spectray\node_modules\mongoose\lib\index.js:238:40)
at Object.<anonymous> (C:\spectray\server.js:15:10)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

Here's my server.js file:

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var flash = require('connect-flash');
var db = require('./config/database.js');

var configDB = require('./config/database.js');

mongoose.connect(db);

app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.set('view engine', 'ejs');

app.use(session({ secret: 'penniesfromheaven', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

require('./app/routes.js')(app, passport);

app.listen(port);
console.log('The magic happens on port ' + port);

What am I dong wrong?


Show source
| javascript   | function   | node.js   | passport.js   2017-01-05 05:01 2 Answers

Answers ( 2 )

  1. 2017-01-05 05:01

    You can add an url value to your database.js

    //database.js
    module.exports =
    {
       url: "mongodb://localhost:27017/sampleDB"
    }
    

    This should be in your app.js

    var db = require('../config/database.js');
    
    mongoose.connect(db.url)
    
  2. 2017-01-06 03:01

    You can simply write following

      var mongoose = require('mongoose');
      mongoose.connect('mongodb://localhost:27017/sampledatabase');
    

    One more thing you can do here is you can create a database.js file and return connection from there and can require that wherever required

◀ Go back