我正在使用 Azure 移动服务开发我的第一个 Windows Phone 应用程序,我使用下面的代码将记录插入到 azure
await App.MobileService.GetTable<TodoItem>().InsertAsync(todo);
检查待办事项是否已存在的最佳方法是什么,插入数据,否则更新服务器端的现有记录?
最佳答案
您可以使用服务器端表脚本在完成插入之前检查记录是否存在。下面是一个示例脚本,用于检查表中的任何项目是否具有匹配的“文本”值,如果是,则不执行插入。
function insert(item, user, request) {
var table = tables.getTable('todoItem');
table.where({
text: item.text
}).read({
success: upsertItem
});
function upsertItem(existingItems) {
if (existingItems.length === 0) {
request.execute();
} else {
item.id = existingItems[0].id;
table.update(item, {
success: function(updatedItem) {
request.respond(200, updatedItem)
}
});
}
}
}
关于如果不存在记录,则 azure 脚本插入,否则在服务器端更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21082490/