TypeError:uri.indexOf不是一个函数

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

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

我什么东错了?

       

网上收集的解决方案 "TypeError:uri.indexOf不是一个函数"

你可以添加一个url值到你的database.js

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

这应该在你的app.js

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

你可以简单地写下面

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

还有一件事你可以在这里做,你可以创build一个database.js文件并从那里返回连接,并可以要求在任何需要的地方