2017-09-25 69 views
-1

我用MongoJS創建了一個快速應用程序。我正在返回一個函數,仍然顯示一個對象已被返回。我甚至在我的JavaScript頁面中添加了module.exports=router。有人能指出這裏失蹤的出口嗎?拋出新的TypeError('Router.use()需要中間件功能,但得到'+ gettype(fn));

app.js

var express = require('express'); 
var define = require('./user/userSlots'); 
var app = express(); 
app.set("views", "./view"); 
app.use('/', define); 
app.listen(3111, function() { 
    console.log('Example app listening on port 3111!'); 
}); 

userSlots.js

var express = require('express'); 
var module = require('../admin/dbModule'); 
var router = express.Router(); 

router.get('/login', function(request, response) { 
    var username = request.params.username; 
    var email = request.params.email; 
    module.authenticateUser(username, email, response); 
}); 
module.exports = router; 

dbModule.js

var mongojs = require("../../node_modules/mongojs"); 
var databaseUrl = "localhost/user"; 
var db = mongojs(databaseUrl); 
var test = db.collection('user_information'); 
var message = null; 
db.user_information.ensureIndex({ email: 1 }, { unique: true }); 
exports.authenticateUser = function(username, email, response) { 
    db.user_information.find({ username: username, email: email }, function(err, users) { 
     if (err || !users) { 
      console.log("Failure"); 
      message = "Failure"; 
      reponse.render('index', { title: 'Hello', message: message }); 
     } else if (users.length == 0) { 
      console.log("Not Valid user"); 
      message = "Failure"; 
      reponse.render('index', { title: 'Hello', message: message }); 
     } else { 
      console.log("valid"); 
      message = "Success"; 
      reponse.render('index', { title: 'Hello', message: message }); 
     } 
    }); 
} 

錯誤:

E:\Vehicles\vehicles.com\node_modules\express\lib\router\index.js:458 

     throw new TypeError('Router.use() requires middleware function but got a ' 
+ gettype(fn)); 
    ^

TypeError: Router.use() requires middleware function but got a Object 
    at Function.use (E:\Vehicles\vehicles.com\node_modules\express\li 
b\router\index.js:458:13) 
    at EventEmitter.<anonymous> (E:\Vehicles\vehicles.com\node_module 
s\express\lib\application.js:220:21) 
    at Array.forEach (native) 
    at EventEmitter.use (E:\Vehicles\vehicles.com\node_modules\expres 
s\lib\application.js:217:7) 
    at Object.<anonymous> (E:\Vehicles\vehicles.com\application\app.j 
s:12:5) 
    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) 

回答

0

你在這一行重新定義module造成了一個問題:

var module = require('../admin/dbModule'); 

而且,然後期待它是原始的,內置module與稍後:

module.exports = router; 

更改此:

var module = require('../admin/dbModule'); 

以不同的名稱:

var dbModule = require('../admin/dbModule'); 

而且,然後用dbModule參考你的數據庫模塊,然後內置module將仍然可以正常使用中module.exports

+0

謝謝。它正在工作。 –

相關問題