【问题标题】:Class variable is undefined... despite being defined类变量未定义...尽管已定义
【发布时间】:2020-02-15 11:06:53
【问题描述】:

我有一个名为 RouteBinder 的类,它看起来像这样:

class RouteBinder
    constructor: (@server, @pool) ->
    bindRoute: (name, fn, method = "post", useDb = true) ->
        @server[method]("/api/accounts/v1/" + name, (req, res, next) ->
            client = await @pool.connect() if useDb?
            await fn req, res, next, client
            await @pool.release() if useDb?
        )

我声明它并这样称呼它:

    binder = new RouteBinder server, pool

    binder.bindRoute "login", controllers.login

(Pool 是node-postgres 的Pool,之前是这样声明和测试的)

    pool = new Pool

    [...]

    try
        client = await pool.connect()
        await client.query 'SELECT 1=1'
    catch e
        console.fatal "Could not connect to database: #{e}"
        return
    finally
        try client.release() if client?
        catch e
            console.fatal "Couldn't release client: #{e}"
            return

    console.info 'Database is OK!'

运行时,我得到这个错误:

 02/14 18:44:34   error   (node:11855) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'connect' of undefined
    at _callee2$ (/home/vi/[redacted]_accounts/main.js:136:38)
    at tryCatch (/home/vi/[redacted]_accounts/node_modules/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (/home/vi/[redacted]_accounts/node_modules/regenerator-runtime/runtime.js:271:22)
    at Generator.prototype.(anonymous function) [as next] (/home/vi/[redacted]_accounts/node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (/home/vi/[redacted]_accounts/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/home/vi/[redacted]_accounts/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /home/vi/[redacted]_accounts/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at /home/vi/[redacted]_accounts/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12
    at /home/vi/[redacted]_accounts/main.js:166:26
 02/14 18:44:34   error   (node:11855) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
 02/14 18:44:34   error   (node:11855) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我正在使用通过 Babel 编译的 CoffeeScript。我的 .babelrc 看起来像这样:

{
  "presets": ["@babel/env"],
  "plugins": [
    ["@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ]
}

对不起,如果这是一个菜鸟问题,我还在学习中,很想得到我能得到的所有建议。

【问题讨论】:

    标签: coffeescript node-postgres


    【解决方案1】:

    我发现了我的错误。 @pool@server 都已定义,但是 @server[method] 的内联函数(处理程序)在该函数的上下文中运行。

    解决方案是使用.bind(@)(或.bind(this),如果您愿意)将其绑定到RouteBinder 实例

        bindRoute: (name, fn, method = "post", useDb = true) ->
            @server[method]("/api/accounts/v1/" + name, ((req, res, next) ->
                console.log "pool", @pool
                client = await @pool.connect() if useDb?
                await fn req, res, next, client
                await @pool.release() if useDb?
            ).bind(@))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      相关资源
      最近更新 更多