title: Uncaught TypeError: Object has no method ... Javascript
body: <p>I'm having an issue where I get an error that says...</p>
  
  <blockquote>
    <p>"Uncaught TypeError: Object f771b328ab06 has no method 'addLocation'" </p>
  </blockquote>
  
  <p>I'm really not sure what's causing this. The <code>'f771b328ab06'</code> is a user ID in the error. I can add a new user and prevent users from being duplicated, but when I try to add their location to the list, I get this error. </p>
  
  <p>Does anybody see what's going wrong? The error occurs in the <code>else</code> statement of the initialize function as well (if the user ID exists, just append the location and do not create a new user). I have some notes in the code, and I'm pretty sure that this is partly due to how I have modified an example provided by another user.</p>
  
  <pre><code>function User(id) {
      this.id = id;
  
      this.locations = [];
  
      this.getId = function() {
          return this.id;
      };
      this.addLocation = function(latitude, longitude) {
          this.locations[this.locations.length] = new google.maps.LatLng(latitude, longitude);
          alert("User ID:" );
  };
      this.lastLocation = function() {
          return this.locations[this.locations.length - 1];
      };
      this.removeLastLocation = function() {
          return this.locations.pop();
      };
  }
  
  function Users() {
      this.users = {};
  
       //this.generateId = function() { //I have omitted this section since I send
          //return Math.random();       //an ID from the Android app. This is part of
      //};                              //the problem.
  
      this.createUser = function(id) {
          this.users[id] = new User(id);
          return this.users[id];
      };
      this.getUser = function(id) {
          return this.users[id];
      };
      this.removeUser = function(id) {
          var user = this.getUser(id);
          delete this.users[id];
  
          return user;
      };
  }
  
  var users = new Users();
  
  function initialize() { 
      alert("Start");
      $.ajax({                                      
          url: 'api.php',                                               
          dataType: 'json',                   
          success: function(data){
              var user_id = data[0];
              var latitude = data[1];
              var longitude = data[2];
  
              if (typeof users.users[user_id] === 'undefined') {
                  users.createUser(user_id);
                  users.users[user_id] = "1";
                  user_id.addLocation(latitude, longitude); // this is where the error occurs
              }           
              else {
                  user_id.addLocation(latitude, longitude); //here too
                  alert(latitude);
              }
          }   
      })      
  }
  setInterval(initialize, 1000);
  </code></pre>
  
  <p>Since I get the ID from the phone and do not need to generate it here (only receive it), I commented out the part that creates the random ID. In doing this, I had to add a parameter to the <code>createUser</code> method within <code>Users()</code> so that I can pass the ID as an argument from <code>Initialize()</code>. See the changes to <code>createUser</code> below:</p>
  
  <p>Before, with the generated ID (the part where the number is generated is in the above code block with comments):</p>
  
  <pre><code>this.createUser = function() {
      var id = this.generateId();
      this.users[id] = new User(id);
      return this.users[id];
  };
  </code></pre>
  
  <p>After, with the ID passed as an argument: </p>
  
  <pre><code>this.createUser = function(id) { 
      this.users[id] = new User(id);
      return this.users[id];
  };
  </code></pre>
  
  <p>If anyone has any suggestions I would really appreciate it. Thanks!</p>