2

I'm just playing around building an API_Routing system that will let me build class focused modules for an API, I'm not necessarily aiming for industry best practices and am just fleshing out ideas on how to build things in javascript for curiosity.

I have a ServerManager class that when the server is run handles all of the processing, this class uses the API_Router class I developed to process the URL that was provided and pass the request to the proper module for handling.

Currently the URL I am using is http://localhost:8080/user/add/23123/

/user/ This tells my program that I am intending on accessing the User Module.

Each Module is able to define, both submodules or functions inside of the routeMapping function.

I have am attaching my User.js file that handles the User Module.

var http = require('http');


//#File: UserManager.js
///http://localhost:8080/user/23123/
///http://localhost:8080/user/add/23123/
var UserManager = {};


UserManager.Create = function() {  
    var UserAcc = {
        "Username": "test123",
        "FirstName": "cody",
        "LastName": "jones",
        "Id": "000001"
    };

    return UserAcc;
}

UserManager.route = function(){
    return 'user';
}

UserManager.routeMapping = {
    "add": this.Create
}

UserManager.Run = function(Data){
    var runMe = Data[0];
    console.log(this.routeMapping);
    console.log(this.routeMapping[runMe]);

}


UserManager.hasSubmodule = false;


module.exports = UserManager;

I am trying to build the routeMapping function to correlate to all of the functions I build for my API, so I need to ensure that the proper function is called based off the URL.

UserManager.routeMapping = {
        "add": this.Create    //What am I doing wrong here
    }

UserManager.Run = function(Data){
    var runMe = Data[0];//This will resolve to a string "add"
    this.routeMapping[runMe]();//This should run the function stored in the routemapping

}
Cody Jones
  • 424
  • 1
  • 5
  • 16

1 Answers1

4

Your use of this is misplaced. The this in that context is of the global scope as your new property is just inside an object, and that object is defined at the global scope.

You can do the following and it should work:

// This is not a function but an object, it doesn't get it's own `this`
UserManager.routeMapping = {
    "add": UserManager.Create  
}

// This is a function, and thus the `this` will be `UserManager`
UserManager.Run = function(Data){
    var runMe = Data[0];
    this.routeMapping[runMe]();
}

As you have UserManager in scope, just reference it's Create property which is also a function.

This is what I think you meant to do:

var UserManager = {};
UserManager.Create = function() {  
    var UserAcc = {
        "Username": "test123",
        "FirstName": "cody",
        "LastName": "jones",
        "Id": "000001"
    };

    return UserAcc;
}

UserManager.route = function(){
    return 'user';
}

UserManager.routeMapping = function () {
        return {
        "add": this.Create
      }
}

UserManager.Run = function(Data){
    var runMe = Data[0];
    var mappings = this.routeMapping();
    return mappings[runMe]();
}

console.log(UserManager.Run(["add"]))

Notice how the routingMapping is a function which then returns the data you want. That one change allows you to use this.

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
  • Just so I understand my error better, when I was using this.Create was I basically trying to reference 'UserManager.routeMapping.Create' which obviously does not exist? – Cody Jones Dec 03 '18 at 21:47
  • Correct. I'm going to update my answer with what I _think_ you might have been really wanting to do. Let me know if it makes sense. – Andrew T Finnell Dec 03 '18 at 21:55
  • The original answer provided the exact solution I needed but that is good to know that I can use a function to make "this" accessible. – Cody Jones Dec 03 '18 at 22:47