2

I have a Sailsjs service class I declared my functions and everything is working fine not until when I tried to access this from within a function to call another, it was null. Doing some googling, I notice calling this in a function would print {}. So, if I'm right with my thought, this in an ES6 arrow function only conforms to the function itself. But, how can I make a SailsJs function written in ES6 Arrow Function access the global this itself. Below is what I tried:

Without ES6 Arrow Functions

const Promise = require('bluebird');

module.exports = {

    uploadFile: function(req) {
        const self = this;
        console.log(self); <-- Prints MainLOG
        console.log(self.logData); <-- Prints "I was able to be reference"
        return new Promise((resolve, reject) => {
            req.file('images').upload({
                dirname: process.cwd() + "/.tmp/Upload"
            }, (error, files) => {
                return error ? reject(error) : resolve(files);
            });
        });
    },

    logData: () => {
        console.log("I was able to be reference");
    }
}


MainLOG: { uploadFile: [Function: wrapper],
  logData: [Function: wrapper],
  identity: 'uploadservice',
  globalId: 'UploadService',
  sails:

  |>   [a lifted Sails app on port 1510]
   \___/  For help, see: http://sailsjs.org/documentation/concepts/

   Tip: Use `sails.config` to access your app's runtime configuration.

   7 Models:
   Bla, Bla, Bla

   8 Controllers:
   Bla, Bla, Bla

   21 Hooks:
   bla, bla, bla
}

ES6 Function

const Promise = require('bluebird');

module.exports = {

    uploadFile: (req) => {
        const self = this;
        console.log(self); <-- Prints {}
        console.log(self.logData); <-- Prints undefine
        return new Promise((resolve, reject) => {
            req.file('images').upload({
                dirname: process.cwd() + "/.tmp/Upload"
            }, (error, files) => {
                return error ? reject(error) : resolve(files);
            });
        });
    },

    logData: () => {
        console.log("I was able to be reference");
    }
}
  • *"`this` in an ES6 arrow function only conforms to the function itself"* No, `this` will refer to whatever `this` refers to outside the function. In a real ES6 module, `this` is `undefined`. In a Node module it refers to `exports` I believe. – Felix Kling Feb 12 '17 at 17:20
  • If that was the case, why is `this` in this scenario equals to `{}` when called in an `arrow function` and equal to the `exports` in normal `function` declaration? @FelixKling –  Feb 12 '17 at 17:34
  • 1
    `this` is not equal to `exports` in a normal function declaration. The value of `this` depends on how the function is called. If the module is imported via `var foo = require('..')` and the function is called via `foo.uploadFile()`, then `this` refers to the value of `foo`, which is the same as `module.exports`. **However**, since you are overwriting `module.exports`, `exports` and `module.exports` refer to *different values*. `exports` in as an empty object, hence you see `{}` when using an arrow function. You can easily test this by adding `console.log(this === exports)` to your function. – Felix Kling Feb 12 '17 at 17:38
  • Thanks for schooling me. Much appreciated. @FelixKling –  Feb 12 '17 at 17:50

0 Answers0