I am trying to accomplish the following task. I have a json array returned from a remote database and I want to iterate over it, check if a record with the ID of the object exists in the local database. If exists, I want to update the record, if it doesn't I want to attach it. The code is goes as follows:
$.each(data, function(idx, task) {
var taskToUpdate = $org.context.Task.attachOrGet({ Id:task.TaskId});
taskToUpdate.TaskType = task.TaskType;
taskToUpdate.StatusId = task.TaskStatusId;
taskToUpdate.TaskStatus = task.TaskStatus;
taskToUpdate.DateScheduled = task.Date;
taskToUpdate.TimeSlot = task.Time;
taskToUpdate.LastUpdated = new Date();
taskToUpdate.TaskName = "Job " + task.TaskId + " " + task.TaskType + " @" + task.AddressOfTask + ", " + task.PropertyPostCode;
taskToUpdate.SpecialInstructions = task.SpecialInstructions;
taskToUpdate.PropertyAddress = task.AddressOfTask;
taskToUpdate.PropertyPostCode = task.PropertyPostCode;
taskToUpdate.PropertyType = task.PropertyType;
taskToUpdate.NumberOfBedrooms = task.NumberOfBedrooms;
taskToUpdate.HasGarage = task.HasGarage;
taskToUpdate.HasOutHouse = task.HasOutHouse;
});
$org.context.saveChanges({
success: function(db) {
that.messages.push("Tasks saved to local device.");
}, error: function(err) {
console.log(err);
that.messages.push("Errors saving tasks: " + err);
navigator.notification.alert("Error saving local tasks to your device!",
function () {
}, "Error", 'OK');
}
});
The code executes successfully but no records are added to the task table.
Am I missing something?