admin管理员组文章数量:1344961
When I run node server.js
in the mand line I get this error:
C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms>node server.js
C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\router\index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\router\index.js:458:13)
at Function.<anonymous> (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\server.js:34:5)
at Module._pile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I think part of it might be that the mongoose.connect
is an unresolved function. Does anyone know how to fix this error?
This is my code:
// Get dependencies
var express = require('express');
var path = require('path');
var http = require('http');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
// import the routing file to handle the default (index) route
var index = require('./server/routes/app');
const messageRoutes = require('./server/routes/messages');
const contactRoutes = require('./server/routes/contacts');
const documentRoutes = require('./server/routes/documents');
// establish a connection to the mongo database
mongoose.connect('mongodb://localhost:27017/cms');
var app = express(); // create an instance of express
// Tell express to use the following parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(logger('dev')); // Tell express to use the Morgan logger
// Tell express to use the specified director as the
// root directory for your web site
app.use(express.static(path.join(__dirname, 'dist')));
app.use('/', index);
app.use('/messages', messageRoutes);
app.use('/contacts', contactRoutes);
app.use('/documents', documentRoutes);
// Define the port address and tell express to use this port
const port = process.env.PORT || '3000';
app.set('port', port);
// Create HTTP server.
const server = http.createServer(app);
// Tell the server to start listening on the provided port
server.listen(port, function() {console.log("API running on localhost: " +
port)});
When I run node server.js
in the mand line I get this error:
C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms>node server.js
C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\router\index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\router\index.js:458:13)
at Function.<anonymous> (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\server.js:34:5)
at Module._pile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I think part of it might be that the mongoose.connect
is an unresolved function. Does anyone know how to fix this error?
This is my code:
// Get dependencies
var express = require('express');
var path = require('path');
var http = require('http');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
// import the routing file to handle the default (index) route
var index = require('./server/routes/app');
const messageRoutes = require('./server/routes/messages');
const contactRoutes = require('./server/routes/contacts');
const documentRoutes = require('./server/routes/documents');
// establish a connection to the mongo database
mongoose.connect('mongodb://localhost:27017/cms');
var app = express(); // create an instance of express
// Tell express to use the following parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(logger('dev')); // Tell express to use the Morgan logger
// Tell express to use the specified director as the
// root directory for your web site
app.use(express.static(path.join(__dirname, 'dist')));
app.use('/', index);
app.use('/messages', messageRoutes);
app.use('/contacts', contactRoutes);
app.use('/documents', documentRoutes);
// Define the port address and tell express to use this port
const port = process.env.PORT || '3000';
app.set('port', port);
// Create HTTP server.
const server = http.createServer(app);
// Tell the server to start listening on the provided port
server.listen(port, function() {console.log("API running on localhost: " +
port)});
Share
edited Apr 5, 2018 at 21:14
user8131921
asked Apr 5, 2018 at 21:05
shae01shae01
611 gold badge2 silver badges5 bronze badges
9
-
Did you try to remove the routes
app.use('foo',fooRoutes)
and add a callback to mongoose.connect to check if the connection is established ? – Mohammed Ilyass NASR Commented Apr 5, 2018 at 21:13 - I have not. How do I add a callback to mongoose.connect? – shae01 Commented Apr 5, 2018 at 21:16
-
mongoose.connect('mongodb://127.0.0.1:27017/trace-vehicle-dev') .then(() => { console.log("Succesfully Connected to the Mongodb Database") }) .catch(() => { console.log("Error Connecting to the Mongodb Database") })
– Mohammed Ilyass NASR Commented Apr 5, 2018 at 21:18 - It still gives me the same error – shae01 Commented Apr 5, 2018 at 21:25
-
I did a little search and found this stackoverflow./a/28379965/7988438 seems like you're missing a
module.exports=router
in one of your routes file – Mohammed Ilyass NASR Commented Apr 5, 2018 at 21:27
2 Answers
Reset to default 5Your mongoose.connect
call is working fine. If it wasn't then you would definitely have received a PromiseRejection
for connection failure and a deprecated warning for UnhandledPromiseRejection
.
Still you can ensure that by adding few event listeners on mongoose events.
mongoose.connect('mongodb://127.0.0.1:27017');
mongoose.connection.on('connected', () => console.log('Connected'));
mongoose.connection.on('error', () => console.log('Connection failed with - ',err));
Coming to your error. This is more likely to happen when you have passed anything but a function as your handler to your app.use
or router.use
call. app.use
and router.use
both require functions to be passed to them which later express
would call whenever a request arrives.
The import statements on top of your code, where you require your routers are most likely to be the culprits here as by default every module.exports
is an object.
I need to look into your router files to further dig into the problem, but you may yourself verify the same. Just see if the module.exports
of every router file imported points to a express router instance - express.Router()
. This way every route file will export a configured express.Router()
instance which will be a function attached to app via app.use()
call.
Replace mongodb://localhost:27017
with mongodb://127.0.0.1:27017
To capture the exact error please follow the below approach.
const mongoose = require('mongoose');
const url = "mongodb://127.0.0.1:27017";
mongoose.connect(url).then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
本文标签: javascriptMongooseconnect not workingStack Overflow
版权声明:本文标题:javascript - Mongoose.connect not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743771640a2536225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论