【发布时间】:2015-05-27 13:50:26
【问题描述】:
我正在制作一个待办事项列表。首次输入项目并将其添加到列表时,服务器运行良好。它接受用户选择的参数并将它们传递到服务器上的列表中,该列表可以通过渲染 Item.list() 来查看,如下所示:
[{"class":"server.Item","id":1,"assignedTo":"User 1","comments":null,"completed":false,"creator":"User 1","name":"Task 1","priority":"1","type":"Personal"},
{"class":"server.Item","id":2,"assignedTo":"User 2","comments":null,"completed":false,"creator":"User 2","name":"Er","priority":"3","type":"Work"},
{"class":"server.Item","id":3,"assignedTo":"User 1","comments":null,"completed":false,"creator":"User 2","name":"Ga","priority":"1","type":"Work"}]
现在,用户可以选择稍后编辑任务。在客户端这工作正常,但我需要用户能够保存新的更新任务。
这是我当前的更新功能:
def updateList() {
def newItem = Item.findById(request.JSON.id)
newItem.assignedTo = request.JSON.assignedTo
newItem.comments = request.JSON.comments
newItem.completed = request.JSON.completed
newItem.creator = request.JSON.creator
newItem.name = request.JSON.name
newItem.priority = request.JSON.priority
newItem.type = request.JSON.type
newItem.save(flush: true)
render newItem as JSON
}
但是,这不起作用。我得到一个空指针异常,上面写着“无法在空对象上设置属性”assignedTo。我假设 findById 请求没有为 JSON 对象获取任何内容,因此没有对象可以分配值,但是我没有'不知道问题是什么考虑到这些项目实际上被放入了 Item.list()。
这是在客户端使用以下JS函数调用的:
$scope.updateList = function() {
angular.forEach($scope.items, function (item) {
// serverList.save({command: 'updateList'}, item);
$http.post('http://localhost:8080/server/todoList/updateList', item)
.success(function(response) {})
.error(function(response) {alert("Failed to update");});
});
};
【问题讨论】:
-
无关 - 避免使用
findById并改用get。它不会影响这个问题,因为它的作用基本相同,但它们的缓存方式非常不同,get的性能会更好。