TypeError:uri.indexOf不是函数

时间:2017-01-05 04:07:51

标签: javascript node.js function passport.js

所以我是JavaScript的新手,我试图在Node.js中构建一个Web应用程序。我想用passport.js将用户身份验证添加到我的应用程序中。但是当我尝试在命令提示符下启动服务器时,我不断收到此错误:

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

这是我的server.js文件:

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);

我错了什么?

2 个答案:

答案 0 :(得分:2)

你可以简单地写下面的

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

您可以在这里做的另一件事是您可以创建一个database.js文件并从那里返回连接,并且可以在需要的地方要求

答案 1 :(得分:1)

您可以将url值添加到database.js

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

这应该在你的app.js

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

mongoose.connect(db.url)