Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TypeError: Cannot read property "length" of undefined.

Posted on 2015-02-04
8
Medium Priority
?
1,655 Views
Last Modified: 2015-02-05
I keep getting the error: TypeError: Cannot read property "length" of undefined.

It seems to happen at the line  searchparams: JSON.stringify(jsonObj)

Can anyone help me find out why?

function runAdvancedSearch() {
    disableAdvancedSearch()
    var resultsDiv = document.getElementById('searchResutsDiv');
    var jsonObj = new Object();
    if ($('#inpAdvSearchCaseId').val().length>0) {
        jsonObj.caseId = $('#inpAdvSearchCaseId').val();
    }
    if ($('#inpAdvSearchCaseNo').val().length>0) {
        jsonObj.caseNo = $('#inpAdvSearchCaseNo').val();
    }
    if ($('#inpAdvSearchCitationNo').val().length>0) {
        jsonObj.citationNo = $('#inpAdvSearchCitationNo').val();
    }
    var dtVal = null;
    if ($('#dtAdvSearchFiledFrom').val().length>0) {
        dtVal = toDateFrom_mmddyyyy($('#dtAdvSearchFiledFrom').val());
        if (isDate(dtVal)) {
            jsonObj.filedBetweenStart = dtVal.getTime();
        }
    }
    if ($('#dtAdvSearchFiledTo').val().length>0) {
        dtVal = toDateFrom_mmddyyyy($('#dtAdvSearchFiledTo').val());
        if (isDate(dtVal)) {
            jsonObj.filedBetweenEnd = dtVal.getTime();
        }
    }
    if ($('#inpAdvSearchCaseLegacyId').val().length>0) {
        jsonObj.legacyId = $('#inpAdvSearchCaseLegacyId').val();
    }
    if ($('#inpAdvSearchArrestNo').val().length>0) {
        jsonObj.arrestNo = $('#inpAdvSearchArrestNo').val();
    }
    if ($('#inpAdvSearchPartyLastName').val().length>0) {
        jsonObj.partyLastName = $('#inpAdvSearchPartyLastName').val();
    }
    if ($('#inpAdvSearchPartyFirstName').val().length>0) {
        jsonObj.partyFirstName = $('#inpAdvSearchPartyFirstName').val();
    }
    if ($('#inpAdvSearchPartyMiddleName').val().length>0) {
        jsonObj.partyMiddleName = $('#inpAdvSearchPartyMiddleName').val();
    }
    if ($('#dtAdvSearchPartyDOB').val().length>0) {
        dtVal = toDateFrom_mmddyyyy($('#dtAdvSearchPartyDOB').val());
        if (isDate(dtVal)) {
            jsonObj.partyDateOfBirth = dtVal.getTime();
        }
    }
    if ($('#inpAdvSearchDriversLicense').val().length>0) {
        jsonObj.driversLicense = $('#inpAdvSearchDriversLicense').val();
    }
    if ($('#inpAdvSearchSSN').val().length>0) {
        jsonObj.ssNo = $('#inpAdvSearchSSN').val();
    }
    if ($('#inpAdvSearchBookingNo').val().length>0) {
        jsonObj.bookingNo = $('#inpAdvSearchBookingNo').val();
    }
    if ($('#attyLastName').val().length>0) {
        jsonObj.attyLastName = $('#attyLastName').val();
    }
    if ($('#attyFirstName').val().length>0) {
        jsonObj.attyFirstName = $('#attyFirstName').val();
    }
    if ($('#attyMiddleName').val().length>0) {
        jsonObj.attyMiddleName = $('#attyMiddleName').val();
    }
    if ($('#barNo').val().length>0) {
        jsonObj.barNo = $('#barNo').val();
    }
    if ($('#judicialOfficer').val().length>0) {
        jsonObj.judicialOfficer = $('#judicialOfficer').val();
    }
    if ($('#magistrate').val().length>0) {
        jsonObj.magistrate = $('#magistrate').val();
    }
    $.ajax({
        url: '/ebench/search/run',
        type: 'POST',
        data : {
            searchparams: JSON.stringify(jsonObj)
        },
        dataType: 'json',
        success: function(data, status, request) {
            switch (request.status) {
                case 200: break;
                case 299: logout(); return;
                default:
                    errorFunction(request);
                    showEmptyAdvancedSearchResults(resultsDiv);
                    enableAdvancedSearch()
                    return;
            }
            try {
                resultsDiv.innerHTML = '';
                if (!data || data===null) {
                    showEmptyAdvancedSearchResults(resultsDiv);
                    enableAdvancedSearch()
                    return;
                }
                var list = jQuery.extend(true, [], data);
                if (list===null || list.length<1) {
                    showEmptyAdvancedSearchResults(resultsDiv);
                    enableAdvancedSearch()
                    return;
                }
                var elem = document.createElement('div');
                resultsDiv.appendChild(elem);
                var el = document.createElement('span');
                elem.appendChild(el);
                el.className = 'bigBoldText';
                el.textContent = 'Total cases found: '+list.length;
                var i=0;
                var divCode = null;
                var filedOn = null;
                var caseStatus = null;
                var style = null;
                for (i=0; i<list.length; i++) {
                    divCode = list[i].divCode;
                    if (divCode===null || divCode.length<1)
                        divCode = 'N/A';
                    filedOn = list[i].filedOn;
                    if (filedOn===null || filedOn.length<1)
                        filedOn = 'N/A';
                    else
                        filedOn = toStrDate_mmddyyyy(new Date(filedOn));
                    caseStatus = list[i].status;
                    if (caseStatus===null || caseStatus.length<1)
                        caseStatus = 'N/A';
                    style = list[i].style;
                    if (style===null || style.length<1)
                        style = 'N/A';
                    /*else
                        style = encodeHtml(style);*/
                    elem = document.createElement('div');
                    resultsDiv.appendChild(elem);
                    elem.className = 'row-fluid caseInfoHeader pointer backgroundColorNavyBlue';
                    elem.textContent = list[i].caseNo+' '+divCode+' '+filedOn+' '+caseStatus;
                    elem.onclick = getCaseFileScopePreserver(list[i].caseId);
                    elem = document.createElement('div');
                    resultsDiv.appendChild(elem);
                    elem.className = 'pointer marginBottom10';
                    elem.onclick = getCaseFileScopePreserver(list[i].caseId);
                    el = document.createElement('span');
                    elem.appendChild(el);
                    el.className = 'docketStyle';
                    el.textContent = style;
                }
            } catch (e) {
                errorFunction(e);
                showEmptyAdvancedSearchResults(resultsDiv);
                enableAdvancedSearch()
                return;
            }
        },
        error: function(data, status, request) {
            switch (data.status) {
                case 200:
                    errorFunction(request);
                    showEmptyAdvancedSearchResults(resultsDiv);
                    enableAdvancedSearch();
                    return;
                case 299: logout(); return;
                default: errorFunction(data.statusText);
            }
        }
    });
}

Open in new window

0
Comment
Question by:onaled777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 29

Expert Comment

by:fibo
ID: 40590720
I would think it says that $('#inpAdvSearchCaseId').val() is undefined.

Maybe you meant $('#inpAdvSearchCaseId').value ?

Something more robust would probably be

if (typeof  $('#inpAdvSearchCaseId').value != "undefined") {...
0
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 1500 total points
ID: 40590897
use your dev tools to figure out exactly which element is throwing the error. i suspect you mistyped one of the selectors.

or, post a link to the page so someone else can find the exact cause of the error.

if you do what fibo is suggesting you'll potentially just mask the error and submit an incomplete form
0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 500 total points
ID: 40591035
Both using the correct functions and the correct variable names are needed.
B-) But using incorrect function names will surely mask any other error
0
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 1500 total points
ID: 40591063
it looks like onaled777 is using jQuery to select elements. In the case of jQuery, the function is val()
0
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 1500 total points
ID: 40591068
@ onaled777 ,

If you're going to use id's and plain vanilla js, then you should probably get rid of the jQuery, in which case the correct property will be 'value' per fibo.

The entire code block is poorly written. If you provide a link to the page, you will probably get better advise.
0
 

Author Closing Comment

by:onaled777
ID: 40591504
Thanks for your input all.  Yes I am in the process of getting more familiar with the debugging tools and was able to find the error the second time around.
0
 
LVL 29

Expert Comment

by:fibo
ID: 40591725
B-) Glad we could help. Thx for the grade and points
0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolve DNS query failed errors for Exchange
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question