Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Farouk Charkas
1,957 PointsURGENT: What is causing this mis-run? (Best Answer Guaranteed)
I have cleared my code of all errors. But one, this one is asking for the following:
Uncaught TypeError: A ParseQuery must be constructed with a ParseObject or class
This has been 'bugging' me for the past hour. Get it? Below is the code I have written so far:
<html>
<center><b><i><u><h1><font color="green">
<head> Welcome to Evergreen!
<title> Evergreen
</title>
<script src="http://www.parsecdn.com/js/parse-1.6.7.min.js"></script>
</head></font></h1></u></i></b></center>
<body bgcolor="white">
<div id="main">
<div style="display:none" class="error">
Looks like there was a problem saving the test object. Make sure you've set your application ID and javascript key correctly in the call to <code>
Parse.initialize</code> in this file.
</div>
<ul id = "list-all-the-facts-given">
</ul>
</div>
<script type="text/javascript">
Parse.initialize("uZXJE7ChTK3XMvs3Nsf8hE5ivHWiGLpEimGAnUp1", "zJ9PQfiin6ZY9FXSqxFvYmuA9uOhsRz64jTWoExL");
var query = new Parse.Query( SomeFacts );
function getInformation() {
console.log("Hi");
var query = new Parse.Query( SomeFacts );
query.find({ success: function( results ) {
var output = "";
for (var i in results) {
var theInformation = results[i].get("actualInformation");
output += "<li>";
output += "<p>" + theInformation + "</p>";
output += "</li>";
}
console.log("I got to line 35, finished function getInformation() declaration.");
$("#list-all-the-facts-given").html(output);
}, error: function( error ) {
alert("There was an error, this error is " + error.message );
}
})
}
getInformation();
var SomeFacts = Parse.Object.extend("SomeFacts");
getInformation();
var theFacts = new SomeFacts();
var supplyInfo = prompt("Enter information: ");
theFacts.set( "actualInformation" , supplyInfo );
theFacts.save(null, {
success: function(theFacts) {
alert('New object created with objectId=: ' + theFacts.id);
},
error: function(theFacts, error) {
alert('Failed to create new object, with error code: ' + error.message);
}
})
</script>
</body>
</html>
1 Answer
Clayton Perszyk
Treehouse Moderator 49,057 PointsHey Farouk,
I was able to fix your code and get it running; I'll add the code below with comments above the code I changed or added. I'll also quickly explain how I got it to work. I first wrapped all of the code in a self invoking anonymous function (if you were using jQuery, you could use $(document).ready(function(){ // your code // });). You need to do this because the DOM has not fully loaded when your JS code runs and thus, in your case, cannot reference the element with an id of 'list-all-the-facts-given'. Next, I noticed their were spaces in your id attribute, which is a quick fix. Within your getInformation() function I used plain JS to add the output content to html (again you could use jQuery, but you must include it in you code). Lastly, I called getInformation() at the end of the script. This last point is what the error you're getting is referring to; this object - var SomeFacts = Parse.Object.extend("SomeFacts"); - needs to be instantiated before you can use SomeFacts in the getInformation() function.
<html>
<center><b><i><u><h1><font color="green">
<head> Welcome to Evergreen!
<title> Evergreen
</title>
<script src="http://www.parsecdn.com/js/parse-1.6.7.min.js"></script>
</head></font></h1></u></i></b></center>
<body bgcolor="white">
<div id="main">
<div style="display:none" class="error">
Looks like there was a problem saving the test object. Make sure you've set your application ID and javascript key correctly in the call to <code>
Parse.initialize</code> in this file.
</div>
<!-- make sure there are no spaces between id="list-all-the-facts-given" -->
<ul id="list-all-the-facts-given">
</ul>
</div>
<script type="text/javascript">
// wrap your code in a self-invoking-anonymous function
(function(){
Parse.initialize("uZXJE7ChTK3XMvs3Nsf8hE5ivHWiGLpEimGAnUp1", "zJ9PQfiin6ZY9FXSqxFvYmuA9uOhsRz64jTWoExL");
function getInformation() {
console.log("Hi");
var query = new Parse.Query( SomeFacts );
query.find({ success: function( results ) {
var output = "";
for (var i in results) {
var theInformation = results[i].get("actualInformation");
output += "<li>";
output += "<p>" + theInformation + "</p>";
output += "</li>";
}
console.log("I got to line 35, finished function getInformation() declaration.");
// unless you have jQuery added to your code $ wont work; you can, however, use plain JavaScript like this.
document.getElementById("list-all-the-facts-given").innerHTML = output;
}, error: function( error ) {
alert("There was an error, this error is " + error.message );
}
})
}
var SomeFacts = Parse.Object.extend("SomeFacts");
var theFacts = new SomeFacts();
var supplyInfo = prompt("Enter information: ");
theFacts.set( "actualInformation" , supplyInfo );
theFacts.save(null, {
success: function(theFacts) {
alert('New object created with objectId=: ' + theFacts.id);
},
error: function(theFacts, error) {
alert('Failed to create new object, with error code: ' + error.message);
}
})
// call getInformation here and nowhere else
getInformation();
})();
</script>
</body>
</html>
Hope this helps; If my descriptions weren't clear enough, just get back to me and i'll try to explain them better.
Best,
Clayton