Changing values of Parse column using Parse Code Cloud Javascript function - javascript

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();
}
})
});

Related

Send bulk email using Parse.com Cloud Code

In the app I'm creating a user will be able to send a description of an object to a number of recipients (from 1 to 200). Using Parse Cloud Code I'll have to use a promise to wait for the email server response for every email (Mailgun).
Is there any other way where I stock all those created emails in some kind of array and send them in 1 shot to the email server? Otherwise I hit the max of 15 seconds a function can run.
Right now I use this:
Parse.Cloud.define("emailobject", function(request, response) {
// ... some company info parameters
var theTraders = request.params.traders;
var objectQ = new Parse.Query("objects");
objectQ.equalTo("objectId", theObjectId);
objectQ.first({
success:function(theObject) {
// ... more code
searchObjectPictures(theObject, {
success:function(pictureObjects) {
for (var a = 0; a < theTraders.length; a++) {
// create the parameters to create the email
var mailingParameters = {};
// ... create the parameters
// when the email html has been compiled
mailgun.sendWelcomeEmail({
to: traderEmail,
from: toString,
subject: subjectString,
html: mailing.getMailingHTML(mailingParameters)
}, {
success:function(httpResponse) {
console.log(httpResponse);
},
error:function(httpResponse) {
console.log(httpResponse);
}
});
}
// emailedObjectsToSave is an array of analytics objects
Parse.Object.saveAll(emailedObjectsToSave, {
success:function(list) {
response.success(true);
},
error:function(error) {
response.error(error);
}
});
},
error:function(error) {
response.error(error);
}
});
},
error:function(error){
response.error(error);
}
});
});
I know promises would be better for nested queries, but I'm still wrapping my head around this.
Thank you
After a lot of searching, trial and error, I have finally found the solution. As #danh said it's better to add these requests to a job queue. The objects are now saved to a "notifications" class that has a bool "notified" to check if this object has already been sent or not.
I'll post the code here for people who might also search for it.
First you add your objects to the new "notifications" class (queue).
Parse.Cloud.define("addToNotificationsQueue", function(request, response) {
var roleName = request.params.role;
var objects = request.params.objects;
var newEmailObjectsToSave = [];
var EmailedObjectClass = Parse.Object.extend("notifications");
var emailObjectACL = new Parse.ACL();
emailObjectACL.setPublicReadAccess(false);
emailObjectACL.setRoleReadAccess(roleName, true);
emailObjectACL.setRoleWriteAccess(roleName, true);
for (var i = 0; i < objects.length; i++) {
// define the parameters for the new objects based on the request params
var emailObject = new EmailedObjectClass();
// set all the details to your new emailObject
emailObject.setACL(emailObjectACL);
newEmailObjectsToSave.push(emailObject);
}
Parse.Object.saveAll(newEmailObjectsToSave, {
success:function(list) {
response.success(true);
},
error:function(error) {
response.error(error);
}
});
});
Then define your job. I run it every 15 minutes for now to test. When the mailserver starts acting faster, I'll set the limit and frequency higher.
Parse.Cloud.job("sendNotifications", function(request, status) {
Parse.Cloud.useMasterKey();
var numberOfNotificationsHandled;
var query = new Parse.Query("notifications");
query.limit(10); // limit because max 15 minutes, slow email server...
query.notEqualTo("notified", true);
query.find().then(function(notifications) {
numberOfNotificationsHandled = notifications.length;
var promise = Parse.Promise.as();
_.each(notifications, function(notification) {
var parameterDict = {};
// add all parameters the emailObject function needs
promise = promise.then(function(){
return Parse.Cloud.run("emailObject", parameterDict).then(function(result) {
notification.set("notified", true);
notification.save();
}, function(error) {
notification.set("notified", false);
notification.save();
});
});
});
return promise;
}).then(function() {
console.log("JOB COMPLETED");
status.success("Sent " + numberOfNotificationsHandled + " emails");
}, function(error) {
console.log("JOB ERROR " + error);
status.error("Job error " + error);
});
});
The emailObject method is just an httpRequest like any other. This method waits for the answers of those httpRequests.

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.

Parsing JSON array of objects: undefined property

So, similar to my my previous question here (I was unsure whether to make a new question or edit the old one), I managed to sucessfully parse the body JSON response using the code seen below.
However when attempting to do the same to the event_calendar_date, I get this: Failed with: TypeError: Cannot read property 'und' of undefined. I would have assumed that to get to the next object in the array the method would be the same for both, however that does not appear to be the case.
JSON response from the server:
[
{
"revision_uid":"1",
"body":{
"und":[
{
"value":"Blah Blah.",
"summary":"",
"format":null,
"safe_value":"Blah Blah.",
"safe_summary":""
}
]
},
"event_calendar_date":{
"und":[
{
"value":"2015-07-20 14:00:00",
"value2":"2015-07-20 20:00:00",
"timezone":"America/New_York",
"timezone_db":"America/New_York",
"date_type":"datetime"
}
]
}
}
]
My Code:
Parse.Cloud.httpRequest({
method: "GET",
url: 'http://www.scolago.com/001/articles/views/articles',
success: function(httpResponse) {
var data = JSON.parse(httpResponse.text);
var articles = new Array();
for (var i = 0; i < data.length; i++) {
var Articles = Parse.Object.extend("Articles"),
article = new Articles(),
content = data[i];
article.set("body", content.body.und[0].value);
article.set("vid", content.vid);
article.set("title", content.title);
article.set("events", content.event_calendar_date.und[0].value);
articles.push(article);
};
// function save(articles);
Parse.Object.saveAll(articles, {
success: function(objs) {
promise.resolve();
},
error: function(error) {
console.log(error);
promise.reject(error.message);
}
});
},
error: function(error) {
console.log(error);
promise.reject(error.message);
}
});
I am afraid you didn't give us all code, or you are using not the code you posted in this question, because it should run fine, as you can see in this demo:
for (var i = 0; i < data.length; i++) {
var content = data[i];
console.log(content.event_calendar_date.und[0].value);
};
EDIT and solution
As I expected, JSON you are getting doesn't have field event_calendar_date for 2nd, and 3rd object. I checked in your full code on GitHub.
First object has correct event_calendar_date field, but next records don't have. See how is JSON you are getting formatted - http://www.jsoneditoronline.org/?id=b46878adcffe92f53f689978873a3474.
You need to check first if content.event_calendar_date is present in record in your current loop iteration or add event_calendar_date to ALL records in JSON response.

Parse.Cloud.job promise not working

What I am trying to do here are:
Remove all contents in a class first, because every day the events.json file will be updated. I have my first question here: is there a better way to remove all contents from a database class on Parse?
Then I will send a request to get the events.json and store "name" and "id" of the result into a 2D array.
Then I will send multiple requests to get json files of each "name" and "id" pairs.
Finally, I will store the event detail into database. (one event per row) But now my code will terminate before it downloaded the json files.
Code:
function newLst(results) {
var event = Parse.Object.extend("event");
for (var i = 0; i < results.length; i++){
Parse.Cloud.httpRequest({
url: 'https://api.example.com/events/'+ results[i].name +'/'+ results[i].id +'.json',
success: function(newLst) {
var newJson = JSON.parse(newLst.text);
var newEvent = new event();
newEvent.set("eventId",newJson.data.id);
newEvent.set("eventName",newJson.data.title);
newEvent.save(null, {
success: function(newEvent) {
alert('New object created with objectId: ' + newEvent.id);
},
error: function(newEvent, error) {
alert('Failed to create new object, with error code: ' + error.message);
}
});
},
error: function(newLst) {
}
});
}
};
Parse.Cloud.job("getevent", function(request, status) {
var event = Parse.Object.extend("event");
var query = new Parse.Query(event);
query.notEqualTo("objectId", "lol");
query.limit(1000);
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var myObject = results[i];
myObject.destroy({
success: function(myObject) {
},
error: function(myObject, error) {
}
});
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
var params = { url: 'https://api.example.com/events.json'};
Parse.Cloud.httpRequest(params).then(function(httpResponse) {
var results = [];
var jsonobj = JSON.parse(httpResponse.text);
for (var i = 0; i < jsonobj.data.length; i++) {
var tmp2D = {"name":"id"}
tmp2D.name = [jsonobj.data[i].name];
tmp2D.id = [jsonobj.data[i].id];
results.push(tmp2D);
}
newLst(results);
}).then(function() {
status.success("run job");
}, function(error) {
status.error(error);
});
});
I think my original answer is correct as a standalone. Rather than make it unreadable with the additional code, here it is made very specific to your edit.
The key is to eliminate passed callback functions. Everything below uses promises. Another key idea is decompose the activities into logical chunks.
A couple of caveats: (1) There's a lot of code there, and the chances that either your code is mistaken or mine is are still high, but this should communicate the gist of a better design. (2) We're doing enough work in these functions that we might bump into a parse-imposed timeout. Start out by testing all this with small counts.
Start with your question about destroying all instances of class...
// return a promise to destroy all instances of the "event" class
function destroyEvents() {
// is your event class really named with lowercase? uppercase is conventional
var query = new Parse.Query("event");
query.notEqualTo("objectId", "lol"); // doing this because the OP code did it. not sure why
query.limit(1000);
return query.find().then(function(results) {
return Parse.Object.destroyAll(results);
});
}
Next, get remote events and format them as simple JSON. See the comment. I'm pretty sure your idea of a "2D array" was ill-advised, but I may be misunderstanding your data...
// return a promise to fetch remote events and format them as an array of objects
//
// note - this differs from the OP data. this will evaluate to:
// [ { "name":"someName0", id:"someId0" }, { "name":"someName1", id:"someId1" }, ...]
//
// original code was producing:
// [ { "name":["someName0"], id:["someId0"] }, { "name":["someName1"], id:["someId1"] }, ...]
//
function fetchRemoteEvents() {
var params = { url: 'https://api.example.com/events.json'};
return Parse.Cloud.httpRequest(params).then(function(httpResponse) {
var results = [];
var remoteEvents = JSON.parse(httpResponse.text).data;
for (var i = 0; i < remoteEvents.length; i++) {
var remoteEvent = { "name": remoteEvents[i].name, "id": remoteEvents[i].id };
results.push(remoteEvent);
}
return results;
});
}
Please double check all of my work above regarding the format (e.g. response.text, JSON.parse().data, etc).
Its too easy to get confused when you mix callbacks and promises, and even worse when you're generating promises in a loop. Here again, we break out a simple operation, to create a single parse.com object based on one of the single remote events we got in the function above...
// return a promise to create a new native event based on a remoteEvent
function nativeEventFromRemoteEvent(remoteEvent) {
var url = 'https://api.example.com/events/'+ remoteEvent.name +'/'+ remoteEvent.id +'.json';
return Parse.Cloud.httpRequest({ url:url }).then(function(response) {
var eventDetail = JSON.parse(response.text).data;
var Event = Parse.Object.extend("event");
var event = new Event();
event.set("eventId", eventDetail.id);
event.set("eventName", eventDetail.title);
return event.save();
});
}
Finally, we can bring it together in a job that is simple to read, certain to do things in the desired order, and certain to call success() when (and only when) it finishes successfully...
// the parse job removes all events, fetches remote data that describe events
// then builds events from those descriptions
Parse.Cloud.job("getevent", function(request, status) {
destroyEvents().then(function() {
return fetchRemoteEvents();
}).then(function(remoteEvents) {
var newEventPromises = [];
for (var i = 0; i < remoteEvents.length; i++) {
var remoteEvent = remoteEvents[i];
newEventPromises.push(nativeEventFromRemoteEvent(remoteEvent));
}
return Parse.Promise.when(newEventPromises);
}).then(function() {
status.success("run job");
}, function(error) {
status.error(error);
});
});
The posted code does just one http request so there's no need for an array of promises or the invocation of Promise.when(). The rest of what might be happening is obscured by mixing the callback parameters to httpRequest with the promises and the assignment inside the push.
Here's a clarified rewrite:
Parse.Cloud.job("getevent", function(request, status) {
var promises = [];
var params = { url: 'https://api.example.com'};
Parse.Cloud.httpRequest(params).then(function(httpResponse) {
var results = [];
var jsonobj = JSON.parse(httpResponse.text);
for (var i = 0; i < jsonobj.data.length; i++) {
// some code
}
}).then(function() {
status.success("run job");
}, function(error) {
status.error(error);
});
});
But there's a very strong caveat here: this works only if ("// some code") that appears in your original post doesn't itself try to do any asynch work, database or otherwise.
Lets say you do need to do asynch work in that loop. Move that work to a promise-returning function collect those in an array, and then use Promise.when(). e.g....
// return a promise to look up some object, change it and save it...
function findChangeSave(someJSON) {
var query = new Parse.Query("SomeClass");
query.equalTo("someAttribute", someJSON.lookupAttribute);
return query.first().then(function(object) {
object.set("someOtherAttribute", someJSON.otherAttribute);
return object.save();
});
}
Then, in your loop...
var jsonobj = JSON.parse(httpResponse.text);
var promises = [];
for (var i = 0; i < jsonobj.data.length; i++) {
// some code, which is really:
var someJSON = jsonobj.data[i];
promises.push(findChangeSave(someJSON));
}
return Parse.Promise.when(promises);

Parse cloudcode beforeSave obtain pre-updated object

In a beforeSave hook I want to obtain the state of the object prior to the update. In this particular case it is to stop a user from changing their choice once they have made it. Pseudo-code looks something like:
If (user has already voted) {
deny;
} else {
accept;
}
And the code that I have so far is:
Parse.Cloud.beforeSave('votes', function(request, response) {
if (!request.object.isNew()) {
// This is an update. See if the user already voted
if (request.object.get('choice') !== null) {
response.error('Not allowed to change your choice once submitted');
}
}
response.success();
}
But request.object is the state of the object with the update already applied.
Note that the 'votes' object is created separately so this isn't allowing an insert but not an update will not suffice; I need to know if a given field is already set in the database.
While Krodmannix's response is correct (and was helpful to me) it has the overhead of a full query. If you are doing things in beforeSave, you really want to streamline them. As a result, I believe a fetch command is much preferable.
Parse.Cloud.beforeSave('votes', function(request, response) {
if (!request.object.isNew()) {
var Votes = Parse.Object.extend("votes");
var oldVote = new Votes();
oldVote.set("objectId",request.object.id);
oldVote.fetch({
success: function(oldVote) {
if (oldVote('choice') !== null) {
response.error('Not allowed to change your choice once submitted');
}
else {
response.success(); // Only after we check for error do we call success
}
},
error: function(oldVote, error) {
response.error(error.message);
}
});
});
If you are using the self hosted Parse Server, there is a property on request called "original" that is the object before changes.
Parse.Cloud.beforeSave("Post", function(request, response) {
console.log(request.object); //contains changes
console.log(request.original); //contains original
response.success();
});
You can use Parse DirtyKeys to identify which field has changed.
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
for (dirtyKey in request.object.dirtyKeys()) {
if (dirtyKey === "yourfieldname") {
response.error("User is not allowed to modify " + dirtyKey);
return;
}
}
response.success();
});
The request variable is the updated row itself. You can get it's object id through request.object.idand use this to grab the current row from the database and check the current value, like so:
Parse.Cloud.beforeSave('votes', function(request, response) {
if (!request.object.isNew()) {
var query = new Parse.Query("votes");
query.get(request.object.id, { // Gets row you're trying to update
success: function(row) {
if (row.get('choice') !== null)
response.error('Not allowed to change your choice once submitted');
response.success(); // Only after we check for error do we call success
},
error: function(row, error) {
response.error(error.message);
}
});
}
This Worked :
var dirtyKeys = request.object.dirtyKeys();
var query = new Parse.Query("Question");
var clonedData = null;
query.equalTo("objectId", request.object.id);
query.find().then(function(data){
var clonedPatch = request.object.toJSON();
clonedData = data[0];
clonedData = clonedData.toJSON();
console.log("this is the data : ", clonedData, clonedPatch, dirtyKeys);
response.success();
}).then(null, function(err){
console.log("the error is : ", err);
});

Categories