【问题标题】:get a TypeError when calling a function by this keyword通过 this 关键字调用函数时得到 TypeError
【发布时间】:2018-11-04 18:05:33
【问题描述】:

我想通过这个关键字在文件中调用一个函数,但我得到了一个 TypeError。 以下是我在一个文件中的部分代码:

router.get('/products', function (req, res){
  try {
    let page = req.query.page || 1;
    let products = Product.paginate({}, { page, sort: { createdAt: 1 }, limit: 12, populate: [{ path: 'categories' }, { path: 'user' }] });
    res.json({
      data: this.filterProductsData(products),
      status: 'success'
    })
  } catch (err) {
    this.failed(err.message, res);
  }
})

function failed(msg , res , statusCode = 500) {
  res.status(statusCode).json({
      data : msg,
      status : 'error'
  })
}

错误文本是:

TypeError: this.failed is not a function
    at C:\Users\Sayyid Ali\Desktop\gheymat\app\routes\v1\home.js:27:10
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:275:10)
    at cors (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:188:7)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:224:17
    at originCallback (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:214:15)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:219:13
    at optionsCallback (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:199:9)
    at corsMiddleware (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:204:7)
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)

我该怎么办? 是因为功能正常吗?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    this is server instance 在 Express 中间件函数中。没有理由将failed 称为this.failed

    如果在此中间件功能范围内可用,则应称为failed

      ...
      } catch (err) {
        failed(err.message, res);
      }
      ...
    

    【讨论】:

    • 哦!我删除了“this”关键字,一切正常。但我不明白为什么。为什么我们不应该在这里使用'this'关键字?它如何理解我们的意思是继续编码的功能?我们应该在哪些地方使用“this”关键字,哪些地方不应该使用?
    • 一个更好的问题是我们为什么要在这里使用this.failed。我看不出任何理由。随机使用this 不会有任何好处。 this 可以是动态上下文(在此处),也可以是词法并从父上下文继承(如果此处的函数是箭头函数,this 将是 global 或未定义)。我建议从基础开始,stackoverflow.com/questions/3127429/…它如何理解我们的意思是继续编码的函数? - 因为failed 在这个函数的范围内可用。你定义了它,它是可用的。
    猜你喜欢
    • 2019-05-26
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多