【问题标题】:Export module correctly with working functions. Node js使用工作功能正确导出模块。节点js
【发布时间】:2017-10-22 05:08:30
【问题描述】:

我想将一些模块导出到我的 app.js 并使用它。但我发现了一个异常:

'getUserFromDB 未定义'

所以,这就是我的 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
}

全栈跟踪:

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

我不知道该怎么办。

【问题讨论】:

  • 您可以使用 api.getUserFromDB() 访问 getUserFromDB 函数
  • 在 API.js 中?我试过了,它不起作用。我也试过使用'this'
  • No..within app.js
  • 看看我的代码,我只在 router.get 中调用 getUserFromDB。我不需要在 app.js 中再调用一次
  • 没关系,我以为你在使用 app.js 时有问题

标签: javascript node.js express node-modules


【解决方案1】:

请尝试以下方法

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

【讨论】:

  • 仍然'getUserFromDB未定义'
  • 我不确定出了什么问题。但是,它对我有用。我刚刚测试过。
  • 您能否确保所有依赖项都已正确安装?
  • 好吧,我一直在尝试从 app.js 中的另一个地方调用 getuserfromdb(我没有看到这些)。请对您浪费的时间表示歉意。
  • 没问题。它发生在每个人身上。无论如何,您可以使用 api 在 app.js 中访问 getUserFromDB。 getUserFromDB()
【解决方案2】:
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
}

【讨论】:

  • 还是一样的异常
  • 在 app.js 中使用 app.use 之前是否需要 express ?
猜你喜欢
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 2014-11-04
  • 2018-12-05
  • 2020-05-26
相关资源
最近更新 更多