Amir
Amir

Reputation: 1683

Javascript query response from Parse.com

I am doing a query from Parse.com through a Javascript function as below.

function doFunction () {
var query = new Parse.Query("english");
query.find({
  success: function(results) {
    alert (results)
  },
  error: function(error) {
    // error is an instance of Parse.Error.
  }
});
}

while I can see the length of query response by alerting results.length, I cant get what is inside the results. alert(results) shows only [object Object],[object Object]...

What is the response format, is it a JSON or an array? how can I get the values?

Thanks

Upvotes: 0

Views: 377

Answers (4)

Amir
Amir

Reputation: 1683

While object.id gives the object id I needed to use object.get('ephrase') to get other parameters.

function doFunction () {
var query = new Parse.Query("english");
query.find({
  success: function(results) {

      alert("Successfully retrieved " + results.length + " scores.");
    // Do something with the returned Parse.Object values
    for (var i = 0; i < results.length; i++) {
      var object = results[i];
      alert(object.id + ' - ' + object.get('ephrase'));
    }

  },

  error: function(error) {
    // error is an instance of Parse.Error.
  }
});

}

Upvotes: 0

jdogg
jdogg

Reputation: 268

While I agree with the above answers that console.log() is a good way to print out your objects, there are better ways to do this. Besides, I recommend that you always use an alert() function in your success and error blocks while in development.

This is because it is possible to have bugs in your code where requests are made to Parse.com an infinite number of times. Since Parse.com will charge you money when you make a certain number of requests per second, this could cause you to accidentally be charged by Parse.com against your wishes. If you are using console.log() and accidentally do this, you won't be aware of the bug unless you have the console open. However, if you use alert(), you will be prompted each time a success or failure call is made, and thus you will be able to prevent this issue.

Furthermore, you don't HAVE to use console.log() to see your data. You can simply call properties on the returned object (which is in JSON format), using the following:

query.find({
   success: function(results) {
      alert(results.get("propertyName"));
   },
   // error blocks and result of logic here

Upvotes: 1

B. Kemmer
B. Kemmer

Reputation: 1537

in javascript you can check objects with a console.log.
console.log is very flexible. It can take n-parameters and every type.
So you can mix up Strings and Objects seperated with a comma.

var myTestObject = { testString: "Hello World!" };
console.log("This is my test Object:", myTestObject);
//Output: This is my test Object: Object {testString: "Hello World!"}

Upvotes: 1

Daniel
Daniel

Reputation: 18692

Use console.log in your code:

function doFunction () {
var query = new Parse.Query("english");
query.find({
  success: function(results) {
    console.log(results);
  },
  error: function(error) {
    // error is an instance of Parse.Error.
  }
});
}

And then see in Developer Tools(F12) -> Console, what is being returned as response.

Upvotes: 1

Related Questions