Not the best moment to ask for a Parse.com question, but I have to keep working with parse for some months. Here is my question:
In Parse.com I'm using the Javascript SDK. I have an array of pointers to class User:
[{userObject1.id}, {userObject2.id}, {userObject3.id}]
how can I delete for example object {userObject2} from inside of the array when I just have the id of the object I want to remove?
Currently I'm removing the inside object by doing a forEach loop using array.splice(indexDelete, 1);. I'm looking for better solution.
var wasSomethingDeleted = 0;
var MyParseFollow = Parse.Object.extend('Follow');
var query = new Parse.Query(MyParseFollow);
query.equalTo("user", channelUser); // fetch all followers
query.first().then(
function (Follow) {
if (Follow){
if (Follow.get("followedBy").length > 0){ // check if there is any follower
var listOfFollowers = Follow.get("followedBy"); // get the array of userObjects of followers
var indexDelete = 0;
listOfFollowers.forEach(function(user){
if( user.id == Parse.User.current().id ){ // I want to remove the current authenticated user
wasSomethingDeleted++;
listOfFollowers.splice(indexDelete, 1); // remove the element
}else{
indexDelete++;
}
});
if( wasSomethingDeleted > 0 ){
Follow.set('followedBy', listOfFollowers); // save new updated array list of followers
Follow.save();
}
}
}
}
);
As per documentation of Parse js guide.Try this
var wasSomethingDeleted = 0;
var MyParseFollow = Parse.Object.extend('Follow');
var query = new Parse.Query(MyParseFollow);
query.equalTo("user", channelUser); // fetch all followers
query.first().then(
function (Follow) {
if (Follow){
if (Follow.get("followedBy").length > 0){ // check if there is any follower
Follow.remove('followedBy', {Parse.User.current().id}); //remove all instances of the current user from followedBy field.
Follow.save();
}
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With