I received a json list and stored it in an array.
{"event_date": "2016-12-07 01:39:41","created_at": "15/11/2016 às 06:10"}
Within this list, there is an attribute called "_date"
: "2016-12-07 01:39:41"
.
I am attempting to extract the year '2016' and the day '07' from this attribute.
$scope.getPostsDate = function() {
PostsService.getPosts($scope.token).then(function(result) {
var postsByDate = result.data;
angular.forEach($scope.postsByDate, function(value, key) {
var oldDate = value.event_date;
value.newDate = oldDate.slice(0, 10);
console.log('date' +value.newDate);
})
})
}
Outcome: TypeError: Cannot read property 'slice' of null at posts.ctrl.js:67 Console output: date 2016-11-28 I am confused because I successfully implemented a similar method before:
$scope.getPosts = function() {
PostsService.getPosts($scope.token).then(function(result) {
$scope.posts = result.data;
angular.forEach($scope.posts, function(value, key) {
var str = value.created_at;
value.data = str.slice(0, 10);
value.hour = str.slice(11, 25);
})
console.log($scope.posts);
})
}
Thank you