Update a new column in a User Class

75 views
Skip to first unread message

Piwet Technologies

unread,
Jun 25, 2020, 11:48:48 AM6/25/20
to Back4App
Hey, i have a user class with more than 500 users registered with information etc, now i decided to create a new column, now how can i update this column for the first 500 users, without having to do the job checking each user field please?

nat...@back4app.com

unread,
Jul 3, 2020, 2:22:21 PM7/3/20
to Back4App
Hi Piwet,

thanks for reaching out!

I'm going to use Javascript SDK to explain my answer, feel free to find more SDKs here :)

In order to get all users who satisfy a query, you must use the find() method with MasterKey (because of the User class). You can use the following code inside a Cloud Code Function to update your users:

const updateAllRows = async(skip, newValue) => {
let User = Parse.Object.extend(Parse.User);
let query = new Parse.Query(User);
query.limit(500);
query.skip(skip);
query.doesNotExist("COLUMN");

let results = await query.find({ useMasterKey: true });

if (!results) { new Error("No user found!");
} else {
for (var i = 0; i < results.length; i++) {
let item = results[i];
item.set("COLUMN", newValue);
}
try {
let saveAll = await Parse.Object.saveAll(results, { useMasterKey: true });

if(results.length >= 500) {
updateAllRows(skip + 500);
} else {
return "Cloud Code executed  successfully!";
}
} catch (e){
new Error(e.message);
}
}
}


Note: I didn't test it directly in your app, so before running the code above on your production app, we highly recommend that you clone your production app and test the code above in this development app first. Once it's tested, you can run it on your production app. If it doesn't work, you can take a look at your logs, going to Server Settings > Logs > Settings.

I hope that the code above is useful for you!

Regards,
Natália.
Reply all
Reply to author
Forward
0 new messages