Parse javascript count method on queries - javascript

In Parse's javascript API, does the count method not do anything if no objects are returned? I'm trying to query for a new table I just added, and I can't seem to get a query to return results.
var contactObj = Parse.Object.extend("Contact");
var contactQuery = new Parse.Query(contactObj);
contactQuery.equalTo("phone", req.body.From);
contactQuery.count({
success: function(number) {
// There are number instances of MyClass.
console.log("something");
},
error: function(error) {
// error is an instance of Parse.Error.
console.log("error");
}
});
In this code, when run, no console.logs are received, but the enclosing method that I call does print that it has been run. Does count not get to success OR failure if the count is 0?

Your are missing the response.success and response.error calls, but console logs are still writing.
See below
Your exact piece of code is returning in your workstation console "success/error was not called" when running.
But still in parse portal console you see "something" output...
Output parse console in your local machine:
{"code":141,"error":"success/error was not called"}
Output parse portal in Logs
Failed with: success/error was not called
I2015-01-14T09:28:26.174Z] something
I'd added below two lines:
response.success("something success");
response.error("something error");
so actual code will be like the one below:
Parse.Cloud.define("StackOverflowTesting", function(request, response) {
var contactObj = Parse.Object.extend("Contact");
var contactQuery = new Parse.Query(contactObj);
contactQuery.equalTo("phone", req.body.From);
contactQuery.count({
success: function(number) {
// There are number instances of MyClass.
console.log("something success console");
response.success("something success");
},
error: function(error) {
// error is an instance of Parse.Error.
console.log("something error console");
response.error("something error");
}
});
});
outputs
workstation console:
{"result":"something success"}
Parse portal Log:
Result: something success
I2015-01-14T09:29:54.355Z] something success console

I had a similar issue where console.logs were not called from success and error blocks. This was caused due to an infinite while loop after the query. Something of this kind -
var c = 0;
var query = new Parse.Query("XXXXX");
query.equalTo("YYYY","abc");
query.count({
success: function(count) {
c += 1;
console.log("success");
},
error: function(error) {
c += 1;
console.log("failure");
}
});
while (c < 1){
}
..........
Node.js is asynchronous but it's also single-threaded. Make sure you do not have any such code blocks which are holding up the execution.
Also, query.count gets to success even if the no. of results is 0.

Related

AJAX silently failing when loading script

I just notice a weird behavior when dynamically loading scripts with AJAX.
I intentionally have a misspelled code that throws an error when it is parsed. Even though the Chrome's console indeed shows the error, the AJAX fail handler is never called.
This is the function that I use to load files:
var load_source = function (path) {
/* Variable used to determine whether the request was successful */
var success = false,
failed;
$.ajax(
{
url: path,
async: false,
dataType: 'script',
method: 'GET'
}
).
done(function () {
success = true;
}).
fail(function (xhr, status, error) {
failed = error.stack ? error.stack : error;
});
if (failed)
{
throw new Error('Unable to load JS file {0}: {1}'.format(path, failed));
}
}
The only variable provided to the load_source function is "path", which value is a string with the location and name of such a file: "js/myFile.js".
The misspelled part (the part with a typo) of the script to load is this:
var f = function (arg) {
var param1 = 3,
param2, /* NOTICE the typo: there is a comma instead of a semicolon */
if (param1 > arg)
{
return true;
}
// And more code is coming next...
If I look at the Chrome's console, it shows the error:
Uncaught SyntaxError: Unexpected token if
So far the only way I can catch the error is with the window's onerror event.
So, if that is the only way to catch the error, could you tell me how to stop the "coding" flow, I mean, whenever I call load_source that throws an error (which is handled by the window's onerror function) I want the script to do nothing else:
load_source('js/myFile.js'); // This script will throw the "Uncaught SyntaxError: Unexpected token if" error.
// So I don't want the following code to be executed.
sum(4, 3);
mul(5, 5);
// ...
Can you guys tell me how make the fail event to be triggered?
I also tried with " $(document).ajaxError" but with the same results.
It's async stuff :) There is no way that the resource fetching (load_source(url)) is going to be finished before your sum() and mul() functions are executed.
The things you want to make dependable on the successful loading of your remote resource, should be placed in a callback, which is executed after success of the resource fetching.
UPDATE
Regarding the "aysnc: false" in your example, and mentioned in the
comments: this applies only to the $.ajax() function scope, not to
the parent function.
Also: is_empty is not part of the standard
library, I assume you have defined that function elsewhere?
The fail event is being triggered, it's just not stopping the rest of the JS from running. You'd be better off doing some thing like this:
if(load_source('js/myFile.js'))
{
sum(4, 3);
mul(5, 5);
}
Then removing your throw in your load_source() function and using return success;, like so:
var load_source = function (path) {
/* Variable used to determine whether the request was successful */
var success = false;
$.ajax(
{
url: path,
async: false,
dataType: 'script',
method: 'GET',
success: function(){
success = true;
},
error: function (xhr, status, error) {
console.log(error.stack ? error.stack : error);
}
}
);
return success;
}

Parse.con Error/success was not called when updating object

I was in trouble because one of my function in Javascript which updates a Parse Object doesn't work but I don't know why !! I have almost the same function which works !
This is my function :
function setRecommendationToDone(user){
var v = document.getElementById("prevent_box");
for(var i=0;i<v.childNodes.length-1;i++){
var checkbox = document.getElementById("checkbox" + i);
if(checkbox.checked){
var id_product = checkbox.parentElement.getAttribute("id");
var query = new Parse.Query("actions");
query.equalTo("user", user);
query.equalTo("id_produit", id_product);
query.first({
success: function(results) {
results.set("Etat","Fait");
results.save();
}, error: function(error){
}
});
}
}
}
Firstly, I thinked It was caused by the fact the save was in a loop but i test it without but still the same error : "success/error was not called". I'm used with this error with Cloud Code but this is not Cloud Code here !!
Thanks for your help.

Parse.com Cloud Code error : TypeError: Cannot call method 'get' of undefined

It is obvious that "result" is coming back as null from the query. If that is the case, why is it calling the "success" routine? I know that the course I am searching for does exist.
Any ideas?
var query = new Parse.Query("Courses");
var CourseObj = new Parse.Object("Courses");
query.equalTo("courseIdFromIOS", request.params.courseIdFromIOS);
query.first({
success: function (result) {
CourseObj = result;
response.success("course lookup good for: " + CourseObj.get("courseName"));
},
error: function () {
response.error("course lookup failed");
}
});
A query always enters success loop if we are able to connect to Parse servers and searched through all the rows even if our query was unsuccessful since there is no error code corresponding to unsuccessful query .Once check this guide and also error codes section.
https://www.parse.com/docs/js/guide#handling-errors
So in your case result is undefined
var query = new Parse.Query("MyClass");
var tmp = new Parse.Object("MyClass");
query.equalTo("username", "This does not exist in table");
query.first({
success: function (result) {
tmp = result;
alert("hii");
alert("course lookup good for: " + tmp.get("name"));
},
error: function () {
alert("helloooo");
}
});
Even in the above code it is entering success loop

undefined variable being returned by JavaScript for unforeseen reason

I'm using JavaScript and parse.com
The below code is not returning any errors in the console log and is creating a new object in parse.com as expected (Under myBadges). But for some reason "BadgeName" is not being captured and is showing as "undefined".
The "BadgeName" column should be populated from the "badgeselected" variable. But "BadgeName" does not appear to being captured as a variable?
Can anyone help me understand why this is happening?
Here is a screen shot of the parse.com backend.
var badgeselected = $("#go").attr("src");
var MyBadges = Parse.Object.extend("myBadges");
var userbadges = new MyBadges();
$(document).ready(function () {
$("#send").click(function () {
userbadges.set("BadgeName", badgeselected);
console.log("done");
userbadges.save(null, {
success: function (results) {
// The object was saved successfully.
location.reload();
},
error: function (contact, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
alert("Error: " + error.code + " " + error.message);
}
});
});
});
Your first line, var badgeselected = $("#go").attr("src");, must also exist inside the $(document).ready callback.
The entire point of that callback is to ensure that the DOM is ready for you to access it. You've put some of your DOM-accessing code inside the callback, but not all of it.

javascript ajax returned value "stolen" in browser console

I've got following javascript code that I execute in the browser:
function run(request) {
var res;
$.ajax({
url:'http://custom-host:8080/',
type: "POST",
async: false,
data: request
}).done(function(data, textStatus, jqXHR){
console.log(textStatus);
res = data;
});
return res;
}
It just asks my custom server for a response that it gets. The Chrome Console log looks like this:
> var a = run({command:'version'}); // executing custom function
success dev.pycached-admin/:14 // this is the console log
undefined // this is the console returned value
> a // ask for value a
"1.1" // value a (returned from ajax)
The question is: how come undefined is returned in the console, when actual value of '1.1' is returned afterwards (the value is correctly assigned)?
If I add debugger statement inside the done function:
}).done(function(data, textStatus, jqXHR){
console.log(textStatus);
debugger;
res = data;
});
then I can see Apple's code which probably maintains the chrome console (VM files in the script tab). Anyway, the AJAX call is synchronous, so why is the value not returned for the first time?
...so why is the value not returned for the first time?
It is, but var is a statement, not an expression, and so it cannot have a result for the console to display. You can see this if you just do this:
> var a = "foo";
undefined
> a
"foo"
So just do this:
> var a
undefined
> a = run({command:'version'});
...which should give you:
success dev.pycached-admin/:14 // this is the console log
"1.1"

Categories