Select to view content in your preferred language

Null field and the trim method for a string

1931
2
08-13-2013 08:07 AM
KennethRichards
Occasional Contributor
In my map a user can select wellheads using a selection box. When they select the wells my updateGrid function collects the desired information from the wellhead feature and displays it in a dojo grid. One of the things this function does is read, split and trim a field called relatedresources that contains multiple urls.

My problem is that my code crashes whenever it tries to read a field that contains a null value. I was able to find a way to compensate for the null values when I try and split a null field but I cannot figure out how to do the same thing when I run my trim method. I get a 'Cannot call method "trim" of undefined.' Im still pretty new to javascript so any ideas, especially simple ideas would be great.

function updateGrid(featureSet){
  
  var data=[];
  var grid = dijit.byId('grid');
  dojo.forEach(featureSet, function (entry) {
   var logs = [], 
    las = [], 
    folders = [],
    
    //relatedResource = entry.attributes.relatedresource || "";
    
    relatedResource = entry.attributes.relatedresource === null ? "" : entry.attributes.relatedresource;
    
    //if (entry.attributes.relatedresource === null) {
    //}
   var raw = relatedResource.split("|");
   raw.forEach(function (bit){
    var resource = bit.split(", ");
// here is where the -> var url = resource[1].trim();
// error occurs  var name = resource[0].trim();
    var anchor = "<li><a href='" + url + "' target='_blank'>" + name + "</a></li>";
    if ( url.indexOf(".tif", url.length -4) !==-1){
     logs.push(anchor);
    }
    if ( url.indexOf(".pdf", url.length -4) !==-1){
     folders.push(anchor);
    } 
    if ( url.indexOf(".las", url.length -4) !==-1){
     las.push(anchor);
    }
   });
   data.push({
    objectid:entry.attributes.objectid,//0
    apino:entry.attributes.apino,//1
    otherid:entry.attributes.otherid,//2
    operator:entry.attributes.operator,//3
    county:entry.attributes.county,//4
    twp:entry.attributes.twp,//5
    rge:entry.attributes.rge,//6
    //headeruri:entry.attributes.headeruri,//7
    section_:entry.attributes.section_,//8
    drillertotaldepth:entry.attributes.drillertotaldepth,//9
    formationtd:entry.attributes.formationtd,//10
    wellname:entry.attributes.wellname,//11
    logField: '<ul>' + logs.join(" ") + '</ul>',
    lasField: '<ul>' + las.join(" ") + '</ul>',
    folderField: '<ul>' + folders.join(" ") + '</ul>'
    //relatedresource: parts[0]
   });

  });
  var dataForGrid= {
   items: data
   };

  var store = new dojo.data.ItemFileReadStore({data:dataForGrid});
  //console.log(store);
  grid.setStore(store);
 }
0 Kudos
2 Replies
ReneRubalcava
Honored Contributor
You were on the right track with this line
if (entry.attributes.relatedresource === null)


You could do a check on the first and second array element, and if it's null or undefined.
if (resource[0] && resource[1]) {
  // continue as normal
}


That should work as it checks for truthiness of those values.
0 Kudos
VinayBansal
Frequent Contributor
You can also use it like this.....
raw.forEach(function (bit){
    var resource = bit.split(", ");
 var url = if(resource[1] !=undefined && resource[1] !="") ?resource[1].trim():"");
  var name = if(resource[0] !=undefined && resource[0] !="") ?resource[0].trim():"");
    
if(url !="")
{
var anchor = "<li><a href='" + url + "' target='_blank'>" + name + "</a></li>";
    if ( url.indexOf(".tif", url.length -4) !==-1){
     logs.push(anchor);
    }
    if ( url.indexOf(".pdf", url.length -4) !==-1){
     folders.push(anchor);
    } 
    if ( url.indexOf(".las", url.length -4) !==-1){
     las.push(anchor);
    }
}
   });
0 Kudos