0

I am learning javascript and was trying out some code below. I am calling the below javascript object as

var client=new atrmsClient('6247543');

var val=client.getRosterData();

I get an error on this line:

var postData= {"cmdShow": "Show", "txtEmpId" : EmpId, "txtPeopleSoft_Id": EmpId, "__VIEWSTATE":  viewState   }

EmpId is undefined. Can anyone tell me what I am doing wrong ?

"use strict";

function atrmsClient(EmployeeId)
{
    this.EmpId=EmployeeId;

    var siteUrl="http://wncrpma011.japa.ad.aexp.com/TransportRoster/EmployeeReport.aspx";


    var getViewState=function()
    {

            $.ajax
        ({
            type: "GET",
            url: siteUrl,
            dataType: 'html',           
            processData: false,
                xhrFields: 
                {
                    withCredentials: true
                }
        })
        .done(ExtractViewState).fail(errorFunc).always(alwaysFunc);

        return "";

    };

    var SendPostRequest=function(viewState)
    {
        var postData= {"cmdShow": "Show", "txtEmpId" : EmpId, "txtPeopleSoft_Id": EmpId, "__VIEWSTATE":  viewState   }

            $.ajax
        ({
            type: "POST",
            url: siteUrl,
            data: postData,
            dataType: 'html',           
            processData: false,
                xhrFields: 
                {
                    withCredentials: true
                }
        })
        .done(parseRosterData).fail(errorFunc).always(alwaysFunc);


    };


    var parseRosterData=function(data)
    {
        console.log(data);



    };

    var ExtractViewState=function(data)
    {

        var rawResponse=data;
        var viewState=$(rawResponse).find('input[name=__VIEWSTATE]')[0].value;
        console.log(viewState);

        SendPostRequest(viewState);

    };

    var errorFunc=function()
    {


    };


    var alwaysFunc=function()
    {


    };

    this.getRosterData=function()
    {
        var viewStateVal=getViewState();
        console.log("calling");
        return "";
    };



}
4
  • You have to access it with this.EmpId Commented May 14, 2014 at 8:43
  • @thefourtheye — Different function. Different value of this. Commented May 14, 2014 at 8:45
  • this.EmpId is not working Commented May 14, 2014 at 8:45
  • @Quentin Oh yeah, its called from a callback. My bad. Commented May 14, 2014 at 8:46

2 Answers 2

3

You never declare a variable called EmpId.

The only EmpId you have is a property of the atrmsClient instance.

Add

var EmpId = EmployeeId;

… or just use EmployeeId as that is still in scope.

Sign up to request clarification or add additional context in comments.

2 Comments

I am confused. Can I not directly access the property of atrmsClient inside that function ?
@MadhurAhuja — You can never access a property of an object (other than the default object (window in a browser), and then only if you aren't using strict mode) directly. If you had a reference to the object in a variable (which, inside that function, you currently don't) then you could access the property through that variable.
1

Another solution could be:

function atrmsClient(EmployeeId) {

    var that = this;
    this.EmpId=EmployeeId;

    var SendPostRequest=function(viewState) {
        var postData= {"cmdShow": "Show", "txtEmpId" : that.EmpId, "txtPeopleSoft_Id": that.EmpId, "__VIEWSTATE":  viewState   }
        // ...
    };

}

It depends if you need EmpId to be available from outside, i.e. using client.EmpId. If yes, then this solution should suit your needs. Otherwise, use @Quentin's.

Comments

Your Answer

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.