1

I wanna call a function in a file by this keyword but I get a TypeError. Here are some parts of my code in one file:

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

And the error text is:

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)

What should I do? Is it because of normal function?

1 Answers1

1

this is server instance inside Express middleware functions. There is no reason to refer to failed as this.failed.

If it's available in the scope of this middleware function, it should be referred as failed:

  ...
  } catch (err) {
    failed(err.message, res);
  }
  ...
Estus Flask
  • 150,909
  • 47
  • 291
  • 441
  • Oh! I deleted 'this' keyword and everything works fine. But I do not understand why. Why should not we use 'this' keyword here? How does it understand that we mean the function that goes on to code? What places should we use 'this' keyword and where shouldn't we do? –  Nov 04 '18 at 12:44
  • A better question would be why we would use `this.failed` here. I see no reasons. Using `this` randomly won't do any good. `this` can be dynamic context (it is here) or be lexical and inherited from parent context (if a function was arrow function here, `this` would be a `global` or undefined). I'd suggest to start with basics, https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work . *How does it understand that we mean the function that goes on to code?* - because `failed` is available in the scope of this function. You defined it, and it's available. – Estus Flask Nov 04 '18 at 12:51