0

I'm currently developing a dashboard which is supposed to show the status of the machines and the logs of their communications. Everything is currently working, except for the communication logs not displaying in the viewMachine modal. It shows me in the modal just "undefined" , however, I've used also the console.log() option and in the Console it does show the information that should be displaying in the modal.

Here you can see how it is currently behaving:

Status Dashboard

and the code is the following:

function readMachineInfo(machine_id){
    var id = machine_id;
    var u = '/machineInfo/' +machine_id;
    $.ajax({
      url: u,
      type: "GET",
      dataType: 'json',
      success: function(data) {
          id = data[0].id;
          name = data[0].name;
          lastComm = convertTime(data[0].lastCommunication);
          response = '<b>' +"Machine ID: " +'</b>' + id.toString();
          response2 = '<b>' +"Machine Name: " +'</b>' + name.toString();
          response3 = '<b>' +"Last Communication: " +'</b>'+lastComm.toString();
          $('#viewMachine .modal-header').html('<b><font color="gray"><h3>' +name.toString() +" info" +'</b></font></h3>');
          $('#viewMachine .modal-body').html(response  +'<p>' +response3 +'<p>' +logs(id));
              }
          });
       }

function logs(machine_id){
      var str = "";
      var URL = 'http://54.201.19.105/read/logs/' +machine_id;
          $.ajax({
             url: URL,
             type: "GET",
             dataType: 'json',
             success: function(data) {
              $.each(data, function(i, item) {
                str = str +"IP: " +item.ipAddress + " DATE/TIME: " +convertTime(item.communicationTime) +" , ";
                })
          console.log(str);
          return str;
             }
          });
}

The readMachineInfo function is the one that shows the basic information inside the Modal such as the Machine ID and the Last Communication, and it is supposed to also show up the result from the logs function. As you can see, in the Console it shows up the results from this function but in the Modal it shows up as "undefined".

Also, I would like to know if there's any way to add this info from the logs function inside the Modal but as a table.

Thanks in advance

natilas12
  • 23
  • 6
  • linked answer is an excellent tutorial also – charlietfl Oct 03 '16 at 23:11
  • as for table you need to parse response to table html – charlietfl Oct 03 '16 at 23:13
  • As long as I can see you don't have any key `ipAddress` in your json but `IP' instead. The same with the date. – Franco Oct 03 '16 at 23:21
  • Actually, I was able to solve it without using callbacks nor promises. I just removed the `return str;` from `logs(machine_id)` function and replaced it with `$('#viewMachine .modal-body p:last').html('

    '+str);` . Also, I changed the `readMachineInfo(machine_id)` function where it says `$('#viewMachine .modal-body').html(response +'

    ' +response3 +'

    ' +logs(id));` to just `$('#viewMachine .modal-body p:last').html('

    '+str);` . Also, I changed the `readMachineInfo(machine_id)` function in success to: `$('#viewMachine .modal-body').html(response +'

    ' +response3 +'

    '); logs(id);`

    – natilas12 Oct 04 '16 at 04:43

0 Answers0