1

I have this simple idea to convert my \n line breaks from my ajax JSON call to <br> so that it would properly display my data.

I tried many different ways to perform the conversion but I keep getting an error saying:

Uncaught TypeError: data.replace is not a function

The issue seems to be the way I'm making this call:

var incident = incident.replace(/\n/g, "<br />");

From what I see on the forums here this is how we're suppose to use .replace but I'm not understading why it doesn't like it.

I tried doing this as well:

"<td colspan='3'>"+incident.Impact.replace(/\n/g, "<br />")+"</td>"+

This works but if the field is empty it returns with an error stating that it cannot read .replace of undefined. Would anyone have any ideas?

Full code:

$.ajax({
    url: this.basePath() + '/GDI_PROD_Incidents?$filter=ÉtatValue%20ne%20%27Fermé%27&$orderby=PrioritéValue desc',
    dataType: 'json',
    cache: false,
    success: function (data) {

        $.each(data.d.results, function (index, incident) {
        var incident = incident.replace(/\n/g, "<br />");

        $('body').append(

          "<table border='1' width='800' >"+
          "<tr  bgcolor='#9aff99' align='center'>"+
          "<td width='800px' colspan='4'><strong>Transfert de connaissances</strong></td>"+
          "</tr>" +
          "<tr bgcolor='#fff'>"+
          "<td width='165'>Billet:</td>"+
          "<td>"+incident.Incident+"</td>"+
          "<td width='165'>Priorité:</td>"+
          "<td>"+incident.PrioritéValue+"</td>"+
          "</tr>"+
          "<tr bgcolor='#fff'>"+
          "<td width='165'>Description:</td>"+
           "<td colspan='3'>"+incident.Description+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td colspan='4' width='800px' bgcolor='#9aff99' align='center'>Détails</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Impact:</td>"+
           "<td colspan='3'>"+incident.Impact+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Dépannage</td>"+
           "<td colspan='3'>"+incident.Dépanage+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td colspan='4' width='800px' bgcolor='#9aff99' align='center'>Suivi</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Prochain Suivi:</td>"+
           "<td colspan='3'>"+incident.Suivi+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Ressource:</td>"+
           "<td colspan='3'>"+incident.Ressources+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Prime:</td>"+
           "<td colspan='3'>"+incident.ResponsableValue+"</td>"+
           "</tr>"+
           "</table>"+
           "<br><br>");

        })

    }
});
JJJ
  • 32,902
  • 20
  • 89
  • 102
Daniel Ellison
  • 1,339
  • 4
  • 27
  • 49
  • Possible duplicate of [jQuery convert line breaks to br (nl2br equivalent)](http://stackoverflow.com/questions/2919337/jquery-convert-line-breaks-to-br-nl2br-equivalent) – rnevius Jan 19 '16 at 15:04
  • You need to replace each field separately. You can't replace everything in the entire object at once. Even better, use a `td { white-space: pre-wrap; }` rule in the CSS so you don't even need
    tags.
    – JJJ Jan 19 '16 at 15:06
  • test value to make sure it is defined and is string before using `replace()` – charlietfl Jan 19 '16 at 15:17

2 Answers2

3

You are trying to use a string method on an object, not on property values within that object.

There is no Object.replace() so that will throw error

Similarly undefined.replace() will also cause error

Presumably you only have to fix a few property values but first it is necessary to make sure those values are defined and are strings before using replace()

Try

function convertBreak(str){
   if(!str){
     return ''; // don't want `undefined` printing into page
   }
   // if it's something other than string, return it as is
   if( typeof str !=== 'string'){
      return str;
   }else{
     return str.replace(/\n/g, "<br />")
    }
}

incident.Description = convertBreak(incident.Description);
charlietfl
  • 170,828
  • 13
  • 121
  • 150
-2

The problem with your code is here:

    var incident = incident.replace(/\n/g, "<br />");

You are redeclaring the variable incident. Try to remove the redeclaration:

    incident = incident.replace(/\n/g, "<br />");