thek04

Null field and the trim method for a string

Discussion created by thek04 on Aug 13, 2013
Latest reply on Aug 13, 2013 by vinaybansal
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);
 }

Outcomes