Problems iterating (angular.forEach) in a JSON subarray

2

I'm trying to access a sub-fix JSON from Angular , but apparently it does not find the nodes of the array.

Code:

var app = angular.module('feedReddit', [])
app.controller('FdRditCtrl', function ($scope, $http) {
	$scope.posts = [];  

	$scope.TraeFeed = function () {
		$http({
			method: 'get',
			url: 'https://www.reddit.com/controversial/.json',
			headers: { 'Content-Type': 'text/json' }
		})
		.then(function (feeds) {
			angular.forEach(feeds.data.children, function(feed) {
				alert(1); //no imprime, no entra aquí
				$scope.posts.push(feed.data);
			});
		});
	};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller = 'FdRditCtrl'>
	<input type="button" ng-click='TraeFeed()' value="Cargar Feed" /><br/><br/>
	<div ng-repeat="post in posts">
		<h2>{{post.title}}	</h2>
	</div>
</div>

However, it does not show the value of the nodes. When I print a value in ForEach to verify if it goes through the array, it does not print anything.

The JSON is too long to publish, but it has this form:

link

{
    "kind": "Listing", "data": {
        "modhash": "", "children": [{
            "kind": "t3", "data": {
                "contest_mode": false, "banned_by": null, "domain": "i.reddituploads.com",
                "subreddit": "pics", "selftext_html": null, "selftext": "", "likes":null,
                "suggested_sort": null, "user_reports": [], "secure_media": null,
                "saved": false, "id": "5qxi4e", "gilded": 0, "secure_media_embed": {},
                "clicked": false, "report_reasons": null, "author": "lidede",
                "media": null, "name": "t3_5qxi4e", "score": 0, "approved_by": null,
                "over_18": false, "removal_reason": null, "hidden": false, "preview": {
                    "images": [{
                        "source": {
"url": "https://i.redditmedia.com/MTyNB6d0XUPZtG3JoU6GB_ZAyI8AO2HzOm7jnOytBIQ.jpg" +
"?s=01b751e19d5e1d5ec9052bb932b274a8",
                            "width": 700, "height": 700
                        }, "resolutions": [{
"url": "https://i.redditmedia.com/MTyNB6d0XUPZtG3JoU6GB_ZAyI8AO2HzOm7jnOytBIQ.jpg" +
"?fit=crop&amp;crop=faces%2Centropy&amp;arh=2&amp;w=108&amp;" +
"s=99a57687775a679d9f3c816ef111121c",
                            "width": 108, "height": 108
                        }, {
"url": "https://i.redditmedia.com/MTyNB6d0XUPZtG3JoU6GB_ZAyI8AO2HzOm7jnOytBIQ.jpg" +
"?fit=crop&amp;crop=faces%2Centropy&amp;arh=2&amp;w=216&amp;" +
"s=6931c650d1b52e61c9811a8ba5f3413e",
                            "width": 216, "height": 216}, {
"url": "https://i.redditmedia.com/MTyNB6d0XUPZtG3JoU6GB_ZAyI8AO2HzOm7jnOytBIQ.jpg" +
"?fit=crop&amp;crop=faces%2Centropy&amp;arh=2&amp;w=320&amp;" +
"s=63e6f58684ed9ef1dc57c0d674b7390d",
                            "width": 320, "height": 320}, {
"url": "https://i.redditmedia.com/MTyNB6d0XUPZtG3JoU6GB_ZAyI8AO2HzOm7jnOytBIQ.jpg" +
"?fit=crop&amp;crop=faces%2Centropy&amp;arh=2&amp;w=640&amp;" +
"s=cf2daabd28b46c4e41b4106644084241",
                            "width": 640, "height": 640}],
                        "variants": {},
                        "id": "SzBl23Ng7BSluwPM-biu6_efl-KfrAvcr1TkVk_cTHQ"
                    }]
                },
"thumbnail": "http://a.thumbs.redditmedia.com/" +
"EMDnkFeuVtpS_KC_AJraNBNSCKtcRE08WOXoOaqI560.jpg",
                "subreddit_id": "t5_2qh0u", "edited": false, "link_flair_css_class": "2",
                "author_flair_css_class": null, "downs": 0, "mod_reports": [],
                "archived": false, "media_embed": {}, "post_hint": "link",
                "is_self": false, "hide_score": false, "spoiler": false,
"permalink": "/r/pics/comments/5qxi4e/and_the_winner_tshirt_of_the_week/",
                "locked": false, "stickied": false, "created": 1485766539.0,
"url": "https://i.reddituploads.com/3bf94e5b844d47028305f3afafb66692?fit=max&amp;h=1536" +
"&amp;w=1536&amp;s=5cbf342bb4a9e373001594b8790433ad",
                "author_flair_text": null, "quarantine": false,
                "title": "And the winner t-shirt of the week...",
                "created_utc": 1485737739.0, "link_flair_text": "US Politics",
                "distinguished": null, "num_comments": 45, "visited": false,
                "num_reports": null, "ups": 0
            }
        }]
    }
}
    
asked by Luis F. Elgueta 31.01.2017 в 15:31
source

1 answer

1

The function then gives you the data in property feeds.data

The json you receive has this format:

{
    "kind": "Listing",
    "data": {
        "modhash": "",
        "children": [
            {}
        ]
    }
}

That means that feeds.data.children does not exist, and what you're looking for is in feeds.data.data.children

    
answered by 31.01.2017 / 18:05
source