-3

Hi I'm getting this error when lighting component was loading.

Uncaught Error in $A.getCallback() [Unexpected token < in JSON at position 0] Callback failed: apex://GDNewsApiDetails/ACTION$GetNewsDetailsforSingleCompany

({
    GetNewsDetails: function(cmp, pageNumber, pageSize) {
        cmp.set("v.Spinner",true);
        //start
        var action = cmp.get("c.GetNewsDetailsforSingleCompany");   
        var sKey;
        var fDateKey;
        var tDateKey;
        var sortKey  = cmp.get("v.sortDirection");
        //console.log('sortKey is ---:'+sortKey );
        var loadFlag = cmp.get("v.onloadFlag");
        if(loadFlag){
            sKey = "";
            fDateKey="";
            tDateKey="";
        }else{
            console.log(cmp.find("fromDateKey").get("v.value"));
            sKey = cmp.find("searchKey").get("v.value");
            fDateKey = cmp.find("fromDateKey").get("v.value");
            tDateKey = cmp.find("toDateKey").get("v.value");
        }
    
        var recId = cmp.get("v.parentId");//for seperate window
        action.setParams({
            "accId":recId,
            "searchKey": sKey,
            "fromDate":fDateKey,
            "toDate":tDateKey,
            "sortKey":sortKey ,
            "pageNumber": pageNumber,
            "pageSize":pageSize
        });        
        action.setCallback(this, function(response) {
            var state = response.getState();console.log('state -----:'+state);
            if (state === "SUCCESS") {    
                //start
                var rs = response.getReturnValue();
                if(rs && rs.news){
                    var labelURL = rs.c360Label;
                    labelURL = labelURL+"/news";
                    var jsonObj = JSON.parse(rs.news);
                    var jsonArrayObj = {"news":{"columns":[],"data":[]}}; 
                    if(jsonObj && jsonObj.Records){
                        jsonArrayObj["news"]["columns"] = [                        
                            // {label: 'Body', fieldName: 'Body', type: 'text'},
                            {label: 'PublishedDate', fieldName: 'PublishedDate', type: 'date',initialWidth: 120} ,
                            {label: 'Title', fieldName: 'TitleLink', type: 'url', typeAttributes: { label: { fieldName: 'Title' }, target:'_blank'}},
                            {label: 'Category', fieldName: 'Category', type: 'text'}
                        ]
                        let news = jsonObj.Records;
                        news.forEach(element => {
                            jsonArrayObj.news.data.push({
                                "NewsArticleId":element.NewsArticleId,
                                "Body": (element.Body?element.Body.replace(/<\s*\/?br\s*[\/]?>/gi, "\n"):""),
                                "Title":element.Title,
                                "TitleLink":labelURL+"/"+element.UrlNode,                        
                                "Category":element.NewsCategory,
                                "Country":element.Country,
                                "PublishedDate":element.PublishedDate//(new Date(element.PublishedDate)).toLocaleString("en-GB", { weekday: "long", year: 'numeric', month: 'long', day: 'numeric' })//(new Date(element.PublishedDate)).toLocaleString()
                            });
                        });     
                    }
                    else {
                        cmp.set("v.Message","No Data Available"); 
                    } 
                    cmp.set("v.jsonArrayObj",jsonArrayObj);
                    cmp.set("v.jsonObj",jsonObj);
                    cmp.set("v.TotalPages",jsonObj.NoOfPages);
                    if(jsonObj.NoOfPages > 0)
                        cmp.set("v.PageNumber",pageNumber);
                    else
                        cmp.set("v.PageNumber",0);
                }
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                            errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
            cmp.set("v.Spinner",false);
        });
        $A.enqueueAction(action);
    }
})
3
  • This issue generally happens when JSON.parse; fails to convert String to a JSON format. Can you paste the value of rs.news ? Jun 23 at 9:22
  • 1
    If you no longer need this question, you can delete it. Leaving the question as it is will likely get it deleted anyways, but it's better to do it yourself.
    – sfdcfox
    Jun 23 at 11:10
  • Chandrashekar, are you interested in the question that you yourself replaced by some garble? Otherwise I'd suggest to delete it. Jun 25 at 18:54

0

Browse other questions tagged or ask your own question.