2017-10-21 112 views
0

我想導出一些模塊到我的app.js並使用它。但是我趕上像一個例外: '定義getUserFromDB心不是'正確導出模塊與工作功能。節點js

所以,這就是我的app.js

var api = require('./routes/API') 
app.use(api.router) 

而且API.js

const router = require('express').Router() 
function getUserFromDB(login){ 
    // some actions 
} 
router.get('/get-db', (req, res) => { 
    getUserFromDB('abcde123') 
}) 
module.exports = { 
    router: router, 
    getUserFromDB: getUserFromDB 
} 

Full Stack trace:

Trace: Trace before app.use(api.router) 
    at Object.<anonymous> (\my-project\build\dev-server.js:74:9) 
    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:389:7) 
    at startup (bootstrap_node.js:149:9) 
    at bootstrap_node.js:502:3 
ReferenceError: getUserFromDB is not defined 
    at NativeConnection.<anonymous> (\my-project\build\dev-server.js:65:5) 
    at NativeConnection.g (events.js:292:16) 
    at emitNone (events.js:86:13) 
    at NativeConnection.emit (events.js:185:7) 
    at open (\my-project\node_modules\mongoose\lib\connection.js:686:11) 
    at NativeConnection.Connection.onOpen (\my-project\node_modules\mongoose\lib\connection.js:695:5) 
    at \my-project\node_modules\mongoose\lib\connection.js:655:11 
    at \my-project\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:71:5 
    at \my-project\node_modules\mongodb\lib\db.js:238:5 
    at Server.connectHandler (\my-project\node_modules\mongodb\lib\server.js:324:7) 
    at Server.g (events.js:292:16) 
    at emitOne (events.js:96:13) 
    at Server.emit (events.js:188:7) 
    at \my-project\node_modules\mongodb-core\lib\topologies\server.js:300:14 
    at \my-project\node_modules\mongodb-core\lib\connection\pool.js:469:18 
    at _combinedTickCallback (internal/process/next_tick.js:73:7) 
    at process._tickCallback (internal/process/next_tick.js:104:9) 
\my-project\node_modules\mongodb\lib\server.js:327 
     process.nextTick(function() { throw err; }) 

我不知道該怎麼做。

+0

下面您可以在使用API​​.js api.getUserFromDB() –

+0

訪問getUserFromDB功能?我試過了,它沒有工作。另外我嘗試使用'this' – ramazan793

+0

No..within app.js –

回答

0

請嘗試以下

const router = require('express').Router() 
function getUserFromDB(login){ 
    // some actions 
    return login; 
} 
router.get('/get-db', (req, res) => { 
    res.send(getUserFromDB('abcde123')); 
}) 
module.exports = { 
    router: router, 
    getUserFromDB: getUserFromDB 
} 

更改到app.js

app.use('/', api.router); 
+0

仍然'getUserFromDB沒有定義' – ramazan793

+0

你的堆棧跟蹤沒有這樣說,對吧? –

+0

我的壞,更新 – ramazan793

0
const router = require('express').Router() 
const getUserFromDB = function(login){ 
    // some actions 
} 
router.get('/get-db', (req, res) => { 
    getUserFromDB('abcde123') 
}) 
module.exports = { 
    router: router, 
    getUserFromDB: getUserFromDB 
} 
+0

仍然同樣的例外 – ramazan793

+0

是否需要在app.js中使用app.use之前表達? –

+0

當然,它的工作項目 – ramazan793