我如何在 nodejs 中将 THIS 与 babel 一起使用?

How can i use THIS with babel in nodejs?

过去我可以使用:

var _this = this
...
exports.someFunction = function (req, res) {
    _this.create(someVar)
}
...
exports.create = (someVar) => {
//logic here
return something
}

但是由于我添加了 BABEL 以使用 GraphQl,package.json:

"devDependencies": {
     "@babel/core": "^7.8.4",
     "@babel/node": "^7.8.4",
     "@babel/preset-env": "^7.8.4",
     "@babel/register": "^7.8.3"
   }

.babelrc 文件:

{
   "presets": [
     [
       "@babel/env"
     ]
   ]
}

我收到错误:

uncaughtException: 无法读取 属性 'create' of null

请让我知道我做错了什么或者我可以在 .babelrc 中做什么以允许使用 这个

谢谢。

因为您想引用一个函数,它是 exports 的 属性,为什么不引用 exports 来访问它?不需要依赖模块的调用上下文:

exports.someFunction = function (req, res) {
    exports.create(someVar);
}

听起来 Babel 模块的顶层没有使用调用上下文调用,所以 thisundefined(就像在严格模式下一样)。