I am trying to figure out how to update an array within a javascript object using an API. As an example, I can pull a full endpoint of /api/posts/:id and get the following results:
我试图弄清楚如何使用API更新javascript对象中的数组。举个例子,我可以提取/ api / posts /:id的完整端点并获得以下结果:
{"_id":"56521356177af21100960105",
"user":"564bbd2da851f0aaeb7746c5",
"description":"'Taking You Higher' Pt. 3 Support here... http://bit.ly/YJBeIg So a year after 'Taking You Higher' Rameses B and I decided to put out another summery progres...",
"title":"'Taking You Higher Pt. 2' (Progressive House Mix) - YouTube","comment":"This is a great mix.","url":"https://www.youtube.com/watch?v=heJBwBUStXU","__v":0,
"created":"2015-11-22T19:11:18.760Z",
"group":["564fddfbf5d334fc0f6b4093"]}
I want to focus on the "group" array within the object. I am looking for a way to update the array by both adding and removing items using AngularJs. As far as I can tell, I will most likely need to use $resource instead of $http. However, I do not know what how this should be written.
我想专注于对象中的“组”数组。我正在寻找一种通过使用AngularJs添加和删除项目来更新阵列的方法。据我所知,我很可能需要使用$ resource而不是$ http。但是,我不知道应该怎么写。
Here is what I have so far in my controller:
这是我到目前为止在控制器中的内容:
myApp.factory('groupsInPost', function($resource) {
return $resource('/api/posts/:id', { id: '@_id' }, {
update: {
method: 'PUT' // this method issues a PUT request
}
});
});
.
myApp.controller('AppCtrl', ['$scope', '$http', '$timeout', '$q', '$log', '$filter', 'groupsInPost',
function($scope, $http, $timeout, $q, $log, $filter, groupsInPost ) {
...
$scope.updateGroup = function(post_id) {
var favorite = $scope.favorite;
$scope.groups = groupsInPost.get({ id: post_id }, function() {
// $scope.groups is fetched from server and is an instance of groupsInPost
console.log($scope.groups.data)
//$scope.groups.$update(function() {
//updated in the backend
});
// $http.put('/api/posts/' + post_id, groups.push(group_id)).success(function(response) {
// refreshPost();
// });
};
...
});
My current problem, as you might immediately expect is that my API is not sending back an array as $resource is expecting. I am not sure where to go from here. If you have any insight, it would be very helpful.
我当前的问题,正如您可能会立即期望的那样,我的API并没有像$ resource期望的那样发回数组。我不知道从哪里开始。如果您有任何见解,那将非常有帮助。
Thank you,
1 个解决方案
#1
0
I got a bit confused on why you call $scope.groups.data
. Is data
part of your API response to the GET
request?
我对你为什么叫$ scope.groups.data感到有些困惑。数据是否是您对GET请求的API响应的一部分?
Also as per the posted code you retrieve a list of groups during your update call. Since the $resource
related to the posts
shouldn't this be done before? At the time you load the posts seems more reasonable.
此外,根据发布的代码,您可以在更新调用期间检索组列表。由于与帖子相关的$资源不应该在此之前完成?当你加载帖子似乎更合理。
On your commented line you push a group_id
to groups list. Where does it come from?
在您的注释行上,您将group_id推送到组列表。它从何而来?
Assuming you want to keep this flow you currently have my suggestion would be something like this:
假设你想保持这种流程,你现在有我的建议是这样的:
var group_id = 1;
$scope.post = groupsInPost.get({ id: post_id }, function() {
$scope.post.group.push(group_id);
$scope.post.$update(function() {
// updated in the backend
});
});
Please let me know if I missed something on the question so I can update my answer accordingly
如果我错过了这个问题,请告诉我,以便我可以相应地更新我的答案
#1
0
I got a bit confused on why you call $scope.groups.data
. Is data
part of your API response to the GET
request?
我对你为什么叫$ scope.groups.data感到有些困惑。数据是否是您对GET请求的API响应的一部分?
Also as per the posted code you retrieve a list of groups during your update call. Since the $resource
related to the posts
shouldn't this be done before? At the time you load the posts seems more reasonable.
此外,根据发布的代码,您可以在更新调用期间检索组列表。由于与帖子相关的$资源不应该在此之前完成?当你加载帖子似乎更合理。
On your commented line you push a group_id
to groups list. Where does it come from?
在您的注释行上,您将group_id推送到组列表。它从何而来?
Assuming you want to keep this flow you currently have my suggestion would be something like this:
假设你想保持这种流程,你现在有我的建议是这样的:
var group_id = 1;
$scope.post = groupsInPost.get({ id: post_id }, function() {
$scope.post.group.push(group_id);
$scope.post.$update(function() {
// updated in the backend
});
});
Please let me know if I missed something on the question so I can update my answer accordingly
如果我错过了这个问题,请告诉我,以便我可以相应地更新我的答案