0

I am trying to populate a table with data from a JavaScript query. Currently, the problem I am running into is that no table is created, and consequently, no data is displayed. A working version of this method can be viewed here. Thanks!

    // Initializes Parse
    // Defines Parse Object Array
    var object = {};

    // Defines Data Query
    var query = new Parse.Query("_User");

    query.find({

    	success: function(results) {

    		// Creates Table in Document
    		var table = document.createElement("table");
    		var row = table.insertRow(0);

    		// Sets Table Attributes
    		table.setAttribute("border", "1");
    		table.setAttribute("width", "100%");

    		// Loops Through Query
    		for (var i = 0; i < results.length; i = i + 1) {

    			// Stores Results
    			var object = results[i];
    			var text = object.get("firstName") + " " + object.get("lastName");
    			var cell = row.insertCell(i);

    			cell.setAttribute("align", "center");
    			cell.appendChild(text);

    		}

    		document.getElementById("main").appendChild(table);

    	},

    	failure: function(error) {

    		alert("Something Went Wrong");

    	}

    });
<!DOCTYPE html>

<html>
    <body>
        <div id="main">
            <!-- Destination -->
        </div>
    </body>
</html>

7
  • 1
    First there was script anchors in the script, there is no need of that and it was making an error. I have corrected that in your snippet. Commented Mar 5, 2016 at 1:32
  • 1
    then you have to load the Parse library Commented Mar 5, 2016 at 1:34
  • Parse is all working fine, I omitted it from the code so one couldn't mess with my app data. Commented Mar 5, 2016 at 5:28
  • My code is pretty much exactly the same as the working code that is linked which is why I am so confused Commented Mar 5, 2016 at 5:29
  • you can use the debugger of your browser to see where there is a problem. And some errors may be displayed is the dev console. Commented Mar 5, 2016 at 5:32

2 Answers 2

0

You have some errors in your code:

  • object.get method does not exists, you can get properties with these ways:

    object.propertyName or object["propertyName"]

JavaScript - Get object property from element data

  • you can not use appendChild to insert a text node, you can use textContent

  • errors are thrown on the console. if you had to use the debugger, put some breakpoints, you would have seen that problems.

https://developer.chrome.com/devtools/docs/javascript-debugging https://developer.mozilla.org/docs/Outils/Débogueur

var success= function(results) {

    		// Creates Table in Document
    		var table = document.createElement("table");
    		var row = table.insertRow(0);

    		// Sets Table Attributes
    		table.setAttribute("border", "1");
    		table.setAttribute("width", "100%");

    		// Loops Through Query
    		for (var i = 0; i < results.length; i = i + 1) {

    			// Stores Results
    			var object = results[i];
    			var text = object.firstName+ " " + object["lastName"];
    			var cell = row.insertCell(i);

    			cell.setAttribute("align", "center");
    			cell.textContent= text;

    		}

    		    
  document.getElementById("main").appendChild(table);

}

datas= [
 { firstName:"f1",lastName:"n1"},
 { firstName:"f2",lastName:"n2"},
 { firstName:"f3",lastName:"n3"},
 { firstName:"f4",lastName:"n4"},
];
  
success(datas);
<!DOCTYPE html>

<html>
    <body>
        <div id="main">
            <!-- Destination -->
        </div>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Object.get method does exist through the Parse library. I figured it out by how the text was appended to the table. Thanks!
0

The problem was with how I appended the text. Instead of cell.appendChild(text);, it should have been cell.textContext = text;.

Here is the final code

// Initializes Parse (Purposely Omitted)


// Defines Parse Object Array
var object = {};

// Defines Data Query
var query = new Parse.Query("_User");

query.find ({

  success: function(results) {

    // Creates Table in Document
    var table = document.createElement("table");
    var row = table.insertRow(0);

    // Sets Table Attributes
    table.setAttribute("border", "1");
    table.setAttribute("width", "100%");

    // Loops Through Query
    for(var i = 0; i < results.length; i = i + 1) {

      // Stores Results
      var object = results[i];
      var text = object.get("firstName") + " " + object.get("lastName");
      var cell = row.insertCell(i);

      cell.setAttribute("align", "center");
      cell.textContent = text;

    }

  document.getElementById("main").appendChild(table);

  },

  failure: function(error) {

    alert("Something Went Wrong");

  }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.