I am usning the following code in Corona to post a table to parse.com (example data).
message1["x1"] = 1
message1["x2"] = 2
message1["y1"] = 3
message1["y2"] = 4
message1["v"] = 5
params.body = json.encode ( message1 )
network.request( baseUrl .. objectClass, "POST", sendresponse, params)
and the following is my beforeSave function to prevent it being save as x1=1.
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
if object.equalTo("1", request.params.x1) {
response.error("x value not valid");
} else {
response.success();
}
});
is this the right way of writing this code? Also does beforeSave run on each object post or I have to call it? Many thanks
You were close... What you would want to do is something like this:
Parse.Cloud.beforeSave("testClass", function(request, response) {
if (request.object.get("x1") === 1) {
response.error("email is required for signup");
} else {
response.success();
}
});
So, in Cloud Code, the object that you are saving is always going to be at "request.object". Since it is a JavaScript object of type Parse.Object, all of the functions outlined at https://parse.com/docs/js/ work (hence the .get("x1")). Once we get the value of the "x1" attribute, we can use standard Javascript evaluators to test equality (in this case, == or === depending on your need).
Also note that "beforeSave" is looking for a String when it comes to the ClassName. Parse.User is the name of the Javascript object class for Parse Users. The User class, in string form, is always "_User".
And yes, this will be called on each POST/save.