我正在尝试完成以下任务。我有一个从远程数据库返回的 json 数组,我想对其进行迭代,检查本地数据库中是否存在具有对象 ID 的记录。如果存在,我想更新记录,如果不存在,我想附加它。代码如下:
$.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');
}
});
代码执行成功,但没有记录添加到任务表中。
我错过了什么吗?