0

I am using AngularJs of $http function, I have one async function need the other one to feedback variable and then to continue ,My code is as follows:

function getDataList(company_id) {
    return $http.get("https://for_test_document/restful_api/brandsearch/format=json&company_id=" + company_id);
}

then I try to use it as follows, the JSON has been received correctly:

for (var i = 0; i < len; i++) {
    getDataList(json.data[i].id).then(function(successdata) {
        var len = successdata.data.length;
        var brand_name = "";
        for (var j = 0; j < len; j++) {
            brand_name = brand_name + successdata.data[j].Brand_Name + '/';
        }
        console.log(brand_name);
        return brand_name + ""
    })

My question is that, the console.log have show what the brand_name correctly, however why in the Browser it shows object? the Browser show as follows

my html is as follows:

function loadInfo(){
    $http({
        method:'GET',
        url:'https://for_example/restful_api/companysearch/?format=json',
        dataType: "json",
        contentType : 'application/json',
        Accept: 'application/json',

    }).then(function (json){
        get_company_type().then(function (data) {
            unique_company_type = data.data

        })
        /*insert data to table */
      var len =json.data.length;
      var tableStr = '';
      tableStr = tableStr+ content
      tableStr = tableStr+'<tbody role="alert" aria-live="polite" aria-relevant="all">';
        for(var i=0;i<len;i++){
            tableStr = tableStr+'<td class=" ">' + 
      getDataList(json.data[i].id).then(function (successdata){                     
       var len = successdata.data.length;                                 
       var brand_name = "" ;                                          
        for(var j=0;j<len;j++){                                    
           brand_name = brand_name +successdata.data[j].Brand_Name+'/';                     
          }                                    
         console.log(brand_name);                                      
        return brand_name + "" }) +                                          
   '</td><td class=" ">' +json.data[i].City  + '</td><td class=" ">' +json.data[i].Address + '</td><td class=" ">' +'Client Contact'  +              
   '</td><td class=" ">' + 'Contact details' + "</td></tr>";
        }
      tableStr = tableStr+"</tbody>";

Thanks for any helps.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Deft-pawN
  • 943
  • 10
  • 12
  • 1
    Can you provide your HTML ? – Serge K. Aug 21 '17 at 08:11
  • binding in table may cause this,because "brand_name = brand_name + successdata.data[j].Brand_Name + '/';" here you are taking value correctly but this may not be in ng-repeat.kindly share the HTML else check the HTML – Sarath Kumar Aug 22 '17 at 12:27

1 Answers1

-1

Clearly it returns object of type brand_name, not sure it's supposed to do that. You can try to parse it forcibly to string using toString() :

for (var i = 0; i < len; i++) {
    getDataList(json.data[i].id).then(function(successdata) {
        var len = successdata.data.length;
        var brand_name = "";
        for (var j = 0; j < len; j++) {
            brand_name = brand_name + successdata.data[j].Brand_Name.toString() + '/';
        }
        console.log(brand_name);
        return brand_name + ""
    })
MirzaS
  • 514
  • 3
  • 16