Reputation: 172
I am trying to update a customers feedback post, the POST & DELETE work but I have been stuck on the PUT for a couple of days. any help will be great. thanks
Factory
app.factory('Post', function ($resource) {
return $resource('/api/apiPost/:id',
{ id: '@id' },
{ 'save': { method: 'POST' } },
{ 'update': { method: 'PUT' } },
{ 'query': { method: 'GET', isArray: false } });
});
CONTROLLER
$scope.updatePost = function (post) {
//alert("WORKS");
Post.update({ id: post.PostId }, $scope.post, function () {
$scope.postArray.push(post);
$scope.post = {
Title: '',
Body: ''
};
}, function () { });
}
ADAPTER
public Post PutNewPost(int id, Post newPost)
{
Post post = new Post();
post.PostId = newPost.PostId;
post.Title = newPost.Title;
post.Body = newPost.Body;
post.Hidden = newPost.Hidden;
db.Posts.Add(post);
db.SaveChanges();
return db.Posts.FirstOrDefault();
}
API CONTROLLER
// PUT api/<controller>/5
[Authorize(Roles = "Admin")]
public IHttpActionResult Put(int id, [FromBody]Post newPost)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok(_adapter.PutNewPost(id, newPost));
}
ERROR MESSAGE
TypeError: undefined is not a function
at h.$scope.updatePost (https://localhost:44301/MyScripts/AjaxPostController.js:40:14)
at https://localhost:44301/Scripts/angular.min.js:169:382
at https://localhost:44301/Scripts/angular.min.js:186:390
at h.$eval (https://localhost:44301/Scripts/angular.min.js:108:40)
at h.$apply (https://localhost:44301/Scripts/angular.min.js:108:318)
at HTMLInputElement.<anonymous> (https://localhost:44301/Scripts/angular.min.js:186:372)
at HTMLInputElement.o.event.dispatch (https://localhost:44301/Scripts/jquery-2.1.0.min.js:3:6055)
at HTMLInputElement.r.handle (https://localhost:44301/Scripts/jquery-2.1.0.min.js:3:2830)
Upvotes: 0
Views: 85
Reputation: 2249
I think your method arguments for $resource are mistaken. Return all your custom methods in 1 object like this:
app.factory('Post', function ($resource) {
return $resource('/api/apiPost/:id',
{ id: '@id' },
{
'save': { method: 'POST' },
'update': { method: 'PUT' },
'query': { method: 'GET', isArray: false }
}
);
Upvotes: 1