Angularjs Grails在尝试更新列表中的项时出现“空指针异常”
我可以毫无问题地将项目添加到我的grails应用程序中,当我显示它们时,它如下所示:Angularjs Grails在尝试更新列表中的项时出现“空指针异常”,angularjs,grails,Angularjs,Grails,我可以毫无问题地将项目添加到我的grails应用程序中,当我显示它们时,它如下所示: [{"class":"server.Item","id":1,"assignedTo":"a","comments":null, "completed":false,"creator":"a","name":"l","priority":"2","type":"Work"}] def updateList() { def newItem = Item.findById(request.JSON.id)
[{"class":"server.Item","id":1,"assignedTo":"a","comments":null,
"completed":false,"creator":"a","name":"l","priority":"2","type":"Work"}]
def updateList() {
def newItem = Item.findById(request.JSON.id)
newItem.name = request.JSON.name
newItem.type = request.JSON.type
newItem.priority = request.JSON.priority
newItem.completed = request.JSON.completed
newItem.comments = request.JSON.comments
newItem.creator = request.JSON.creator
newItem.assignedTo = request.JSON.assignedTo
newItem.save(flush: true)
render newItem as JSON
}
只是一个JSON对象的列表,它工作得很好,在我的客户机中保持不变,等等
但是,当我尝试更新时,会出现空指针异常:
2015-05-26 11:56:33,504 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver -
NullPointerException occurred when processing request:
[OPTIONS] /server/todoList/updateList
Cannot set property 'name' on null object. Stacktrace follows:
Message: Cannot set property 'name' on null object
我的更新控制器如下所示:
[{"class":"server.Item","id":1,"assignedTo":"a","comments":null,
"completed":false,"creator":"a","name":"l","priority":"2","type":"Work"}]
def updateList() {
def newItem = Item.findById(request.JSON.id)
newItem.name = request.JSON.name
newItem.type = request.JSON.type
newItem.priority = request.JSON.priority
newItem.completed = request.JSON.completed
newItem.comments = request.JSON.comments
newItem.creator = request.JSON.creator
newItem.assignedTo = request.JSON.assignedTo
newItem.save(flush: true)
render newItem as JSON
}
它在我的角度控制器中被称为:
$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");});
});
};
以下是我的项目的属性:
name: $scope.thing,
type: $scope.worktypeSelection,
priority: $scope.prioritySelection,
completed: false,
comments: '',
creator: $scope.username,
assignedTo: $scope.assign.name
从angular调用时,项中是否有id?否,此处声明项:pastebin可能无法访问。向问题添加内容。您确定您按ID加载的项目确实在数据库中,因此不为空吗?我怀疑是分配newItem.name=request.JSON.name导致代码失败。如果我将Item与Item.list一起列出,则所有适当的项都在那里。