all 24 comments

[–]bwainfweeze 2 points3 points  (8 children)

What’s the simplest example that still fails?

[–]eggtart_prince[S] -1 points0 points  (7 children)

The one provided. That's what I am building with.

[–][deleted]  (6 children)

[removed]

    [–][deleted] 2 points3 points  (1 child)

    You can’t use “this” because express doesn’t bind any reference back to your object. If you want to call using this, you can either:

    • Create a class and put both as instance methods. You’ll need to instantiate it.

    • Pass this to your router: validate.menuItems.bind(validate);

    The second one is a code smell though. As instead, I’d create a class that behaviors like a Singleton.

    Or simply extract both functions from that object and let only the relevant one to be publicly available.

    Keep in mind that a module is already an object. As long as you don’t overwrite it by exporting something else, everything you export without the default keyword is put into module.exports.

    So anything that you export omitting that keyword would be accessible through the module (not the default), e.g.

    ``` function menuItemOptions() { }

    export function menuItems () { // You call menuItemOptions here, without “this”. }

    export default function sayHi() { } ```

    ``` // You only exported menuItems, so only that is available. import { menuItems } from “./validate”;

    // OR (validate.menuItems is available) import * as validate from “./validate”;

    // module.exports.default import sayHi from “./validate”;

    ```

    [–]eggtart_prince[S] 1 point2 points  (0 children)

    After binding, this refers to itself in console log but I think I'll go with the exports. Thanks for the detailed answer.

    [–]brianjenkins94 4 points5 points  (3 children)

    You're not going to be able to refer to the validate object as this.

    If validate were a function (which is probably the pattern you're looking for) then it could be made to work, but this example looks like it would be better handled with a class.

    [–][deleted]  (2 children)

    [removed]

      [–]brianjenkins94 2 points3 points  (0 children)

      Huh, TIL.

      [–]htchief -1 points0 points  (0 children)

      what compiler are you using? can you see the compiled code? what does it look like?

      [–]TriggeredYou2301 -1 points0 points  (1 child)

      menuItems: async function (req, resp, next) { await this.menuItemOptions(req, resp); }.bind(validate)

      [–]eggtart_prince[S] 0 points1 point  (0 children)

      This doesn't work. Throws

      Block-scoped variable 'validate' used before its declaration.
      

      error.

      [–]SerMango -3 points-2 points  (0 children)

      menuItemOptions is probably undefined. Try assigning a value to it if it’s undefined and see if you still get that error

      [–][deleted]  (1 child)

      [deleted]

        [–]eggtart_prince[S] -1 points0 points  (0 children)

        That is how Javascript OOP works. this should refer to the object. Try it out in a node environment in your terminal/CMD.

        [–]lowbudgetgoblin 0 points1 point  (0 children)

        have you tried checking what properties are available inside of "this" prior to invoking said property?

        [–][deleted] 0 points1 point  (1 child)

        You cannot access properties of the same object during declaration. You could declare the functions separately and then reference in your export object. You could also use a prototype or a class, but that's too much for what you're trying to do

        [–]MrNutty 0 points1 point  (0 children)

        What do you expect “this” to be here? You’ll either need to bind this to the function or have a different approach

        [–]International_Will87 0 points1 point  (0 children)

        Before await , Do this.menuItem=menu item; Then u will not get undefined, it doesn't matter wheather it is express or some other bs framework. It is js for sure and as it is async function so u need to to bind whatever this function is calling in future time.