Concat is not a function

hello,
been trying to implement pull to refresh and infinite scroll to use in a list that 's populated from a json file
followed a tutorial :http://mcgivery.com/creating-feed-ionic/.it was helpful but i don’t know how to get past the concat error.

See the Pen zZbMvd by mohamed reda (@redamed) on CodePen.

thank you

Hi!

concat is only a valid function for arrays or strings. Almost sure you try to concat an object or number. Try to log what the service is returning and check its type

yeah it s true.getting undefined related to the line.i followed the tutorial.in the service
items = response.data.results; change it to items=response.data.with that being fixed i still got the same concat error.
here’s what i get in the console just to give you an idea
image

Ok, the service is returning Object {news: Array(10)}, an Object, that contains an array (news). concat can not be applied to Objects, so you need to do something like

$scope.items = $scope.items.concat(items.news)

EDIT: Seen your codepen you posted before it would be

$scope.articles = [];

$scope.loadMore=function() {
    var page=2;
    article.getmorearticles(page).then(function(items) {
        $scope.articles=$scope.articles.concat(items.news);
        $scope.$broadcast('scroll.infiniteScrollComplete');
        page++;
    })
}

Of course $scope.articles must be initialized as an array before you concatenate it

I think you want to concat in the news array. If that is the case, you can do items = response.data.news.

@lucasbasquerotto @Adonai. so i made the changes.still getting the same error
image
scope.articles is initialized in the controller.then i used it in ng-repeat to display in a list

Can you share your full controller code?

Also…

Check if $scope.articles is an array

console.log(angular.isArray($scope.articles));

Must return true;

here it is with the factory andthe controller

See the Pen oZVmRE by mohamed reda (@redamed) on CodePen.

Thanks for sharing. I think I see the problem now

You call this function to get the articles.

article.getArticles().then(function(response){
            $scope.articles = response.data;
             .....

when the API call finish, you overwrite your previous variable $scope.articles = [] to $scope.articles = response.data;

but… as we have seen before is response.data.news because response.data return an Object (that not have concat function)

I think changing your article.getArticles() function to

article.getArticles().then(function(response){
   console.log(response.data);
   $scope.articles = response.data.news;
})

will work.

Anyway add console.log(response.data); to check exactly what is returning

1 Like

thanks for the help dude.it’s working.really appreciate it (y)

1 Like