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

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.

Related

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

Call update in Parse Cloud Code

I know there were similiar questions, but I see no solution in them.
I want to create new object if it doesn't exists in database, and update if one exists.
Here is my simple code :
Parse.Cloud.beforeSave("Tag", function(request, response) {
var query = new Parse.Query("Tag");
query.equalTo("name", request.object.get("name"));
query.first({
success: function(result) {
if (!result) {
response.success();
} else {
result.increment("popularityCount");
result.save();
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
As you see, I am calling it beforeSave. If query doesn't find anything, creates new entry. If query finds something, it should take this result, and popularityCount. But it doesn't. It works only if I call response.success() after that, but calling this function causes also in creating new entry.
It seems wrong to increment a counter on an object on every save. What if the object is modified for some other reason? If you really do want to increment a field on every save, there's no need for a query -- the object being saved is passed to the function. Moreover, a query will not work in the case where a new object is being saved.
How about instead, find or create the object as one operation, increment the counter when app logic calls for it
function findOrCreateTagNamed(name) {
var query = new Parse.Query(Tag);
query.equalTo("name", name);
return query.first().then(function(tag) {
// if not found, create one...
if (!tag) {
tag = new Tag();
tag.set("popularityCount", 0);
tag.set("name", name);
}
return (tag.isNew())? tag.save() : Parse.Promise.as(tag);
});
}
function incrementPopularityOfTagNamed(name) {
return findOrCreateTagNamed(name).then(function(tag) {
tag.increment("popularityCount");
return tag.save();
});
}
Now there's no need for beforeSave logic (which seems like the right thing to do, not a workaround).
Parse.Cloud.beforeSave("Tag", function(request, response) {
var tag = request.object;
tag.increment("popularityCount");
response.success();
});

Parse javascript count method on queries

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.

Changing values of Parse column using Parse Code Cloud Javascript function

So its my first time writing Javascript so please bear with me. I wrote this function in order to query a class in my Parse.com application, and then after querying I want to set one of the columns (of type Boolean) to true.
I set up a test class with only 7 values in order to test.
The problem: only 3 out of 7 are being changed. Do I have to wait after each save? I know that waiting/sleeping in Javascript is "wrong" but I can't seem to find a solution.
Thanks in advance!
Additionally, when using iOS/Parse, I would like to check if the boolean value is undefined in Objective-C, I already tried to compare it to nil/NULL, an exception was thrown
Parse.Cloud.define("setYears", function(request, response) {
var object = new Parse.Query("testClass");
object.find({
success: function(results)
{
for (var i = 0; i < results.length; i++) {
results[i].set("testBool",true);// = true;
results[i].save(null,
{
success:function ()
{
response.success("Updated bool!");
},
error:function (error)
{
response.error("Failed to save bool. Error=" + error.message);
}
});
};
response.success();
}
})
});
It turned out to be not that difficult to solve as stated above. Just had to use saveAll instead of saving each object by itself. Here is the correct solution if anybody needs it:
Parse.Cloud.define("setYears", function(request, response) {
var object = new Parse.Query("testClass");
object.find({
success: function(results)
{
for (var i = 0; i < results.length; i++) {
results[i].set("testBool",true);// = true;
}
Parse.Object.saveAll(results,{
success: function(list) {
// All the objects were saved.
response.success("ok " ); //saveAll is now finished and we can properly exit with confidence :-)
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error("failure on saving list ");
},
});
response.success();
}
})
});

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.

Categories