For my current project, I am developing an Ionic app with Parse.com as the backend. My goal is to save an item to an existing Object in the database. If the item already exists, I want to update it instead of creating a new entry every time. I have been using the .save method from Parse documentation, but it keeps generating new rows.
To query for the existing ID, I use the following code:
var ExistingId = Parse.Object.extend("Exercises");
var query = new Parse.Query(ExistingId);
query.equalTo("exerciseName", $scope.currentExcercise);
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var ExistingIdNumber = object.id;
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
This is the code I use to create or save rows in the Exercises
Object:
$scope.saveToParse = function(exercise) {
var Exercises = Parse.Object.extend("Exercises");
var exercises = new Exercises();
exercises.set({exerciseName: exercise.ExerciseName,
exerciseID: exercise.ExerciseID,
exerciseDescription: exercise.Description,
sets: exercise.Sets,
reps: exercise.Reps,
resistance: exercise.Resistance,
tempo: exercise.Tempo,
images: $scope.images})
exercises.save(null, {
success: function(exercises) {
alert('Exercise saved with objectId: ' + exercises.id);
},
error: function(exercises, error) {
alert('Failed to create new object, with error code: ' + error.message);
}
});
}
I am looking for a way to combine these two functions to automatically check for existing values and either create or update them.
Best regards, Simon