繁体   English   中英

Mongoose.connect 不工作

[英]Mongoose.connect not working

当我在命令行中运行 node server.js时,出现此错误:

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._compile (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

我认为部分原因可能是mongoose.connect是一个未解决的函数。 有谁知道如何解决这个错误? 这是我的代码:

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

您的mongoose.connect通话正常。 如果不是,那么您肯定会收到针对连接失败的PromiseRejection和针对UnhandledPromiseRejection的已弃用警告。

您仍然可以通过在猫鼬事件上添加少量事件侦听器来确保这一点。

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

出现您的错误。 当您将除函数以外的任何内容传递给app.userouter.use调用时,更可能发生这种情况。 app.userouter.use都要求将传递给它们的函数传递给它们,稍后express将在请求到达时调用这些函数。

代码顶部的import语句很可能是罪魁祸首,在默认情况下,每个module.exports都是您在此处需要路由器的罪魁祸首module.exports是一个对象。

我需要调查您的路由器文件以进一步研究问题,但您可能自己也对此进行了验证。 只需查看每个导入的路由器文件的module.exports是否指向Express路由器实例express.Router() 这样,每个路由文件都将导出配置的express.Router()实例,该实例将是通过app.use()调用附加到app的函数。

mongodb://localhost:27017替换为mongodb://127.0.0.1:27017

要捕获确切的错误,请遵循以下方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM