0

When using this Code the function returns undefined instead of an object.

function Oauth_User_Profile() {
 this.username = "=";
 this.password = "=";
 this.refresh_token = "=";
 this.access_token = "=";
 this.request = function(url, formData, method, then) {
 var xhttp = new XMLHttpRequest();
 xhttp.onreadystatechange = function() {
 if(xhttp.readyState == 4 && xhttp.status == 200)
 {
 var j = JSON.parse(this.responseText);
 then(j);
 }
 }
 xhttp.open(method, url); 
 xhttp.send(JSON.stringify(formData)); 
 }
}
var oauth_user_profile = Oauth_User_Profile();
oauth_user_profile.username = "sample";

I've got nearly the same Code for other classes which is working fine, so I don't know why this isn't running properly

Duplicate: Question was marked as a duplicate for [How do I return the response from an asynchronous call?. However, I don't want to return the result of the call, I want to return an object which contains username, password, refresh_token... AND a method which can be run. I run into the exact same problem when removing the async-part:

function Oauth_User_Profile() {
 this.username = "=";
 this.password = "=";
 this.refresh_token = "=";
 this.access_token = "=";
}

This still gives the error: Uncaught TypeError: Cannot set property 'username' of undefined at testm.html:67

Wonko the Sane
10.8k8 gold badges72 silver badges97 bronze badges
asked Nov 12, 2018 at 8:34
2
  • You forgot to call the constructor function with new Commented Nov 12, 2018 at 8:37
  • Just for good standards, in JavaScript we use UpperCamelCase for classes: Oauth_User_Profile => OauthUserProfile and lowerCamelCase for variables and functions: oauth_user_profile => oauthUserProfile Commented Nov 12, 2018 at 8:39

1 Answer 1

3

You are missing the new keyword:

var oauth_user_profile = new Oauth_User_Profile();
answered Nov 12, 2018 at 8:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.