Chris6743166
Posts: 1
Joined: Tue Mar 18, 2014 11:50 am

.filter problem: TypeError: 'undefined' is not a function

I'm trying to retrieve a specific news item from a JSON array that's stringified and saved in localstorage.

I created this generic service:

codeAppery.get_cache_news_item_js = Appery.createClass(null, {

Code: Select all

 init: function (requestOptions) { 
     this.__requestOptions = $.extend({}, requestOptions); 
 }, 

 process: function (settings) { 
     settings.beforeSend(settings); 
     if (this.__requestOptions.echo) { 
         settings.success(this.__requestOptions.echo); 
     } else { 
         var cdata = JSON.parse(localStorage.getItem("json_news")); 
         var match = cdata.filter(function (item) { 
             return item.posts.id === settings.data.post_id; 
         }); 
         settings.success(match); 
         settings.complete('success'); 
     } 
 } 

});/code

This is the data that's stringified and saved to localstorage as 'json_news':
http://schoolsays.co.uk/schools/acref...

The service includes a request parameter named 'post_id' which I've mapped to a localstorage item of the same name.

However, I'm getting the error "TypeError: 'undefined' is not a function" here:

Image

Can anyone see where I'm going wrong here?

The app is shared with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a if you want to take a look at the mapping.

Thanks!

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

.filter problem: TypeError: 'undefined' is not a function

Hi Chris,

It seems, 'cdata' doesn't contain a function 'filter'. You can check the content of variable 'cdata' in debugger using breakpoint, or output it in console right after declaring it.

Chris6743166
Posts: 1
Joined: Tue Mar 18, 2014 11:50 am

.filter problem: TypeError: 'undefined' is not a function

Hi Katya,

Thanks for the response.

Excuse my non-coder ignorance, but I thought 'filter' was a method for filtering an array.

I'm trying to filter the array created by parsing json_news in order to then extract a single news item.

I wrote the service based on previous advice provided here:

https://getsatisfaction.com/apperyio/...

Am I not going about it in the right way?

Here's what I see using breakpoints:

Image

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

.filter problem: TypeError: 'undefined' is not a function

Hello Chris,

As we can see from your picture variable "posts" is array in variable "cdata". Could you detail what you want to get and what data you have in local storage variable "json_news".

Chris6743166
Posts: 1
Joined: Tue Mar 18, 2014 11:50 am

.filter problem: TypeError: 'undefined' is not a function

I want to get the title, content and date of a single post from posts, which is a subarray of cdata. I have the post id mapped as a request parameter and title, content and date mapped as response parameters.

Chris6743166
Posts: 1
Joined: Tue Mar 18, 2014 11:50 am

.filter problem: TypeError: 'undefined' is not a function

I seem to have solved the issue with this:

code
Appery.get_cache_news_item_js = Appery.createClass(null, {

Code: Select all

 init: function (requestOptions) { 
     this.__requestOptions = $.extend({}, requestOptions); 
 }, 

 process: function (settings) { 
     settings.beforeSend(settings); 
     if (this.__requestOptions.echo) { 
         settings.success(this.__requestOptions.echo); 
     } else { 
         var post_id = localStorage.getItem("post_id"); 
         var json_news = localStorage.getItem("json_news"); 

         var cdata = JSON.parse(json_news); 

         cdata = jQuery.grep(cdata.posts, function(element, index){ 
             return element.id == post_id; 
         }); 

         settings.success(cdata); 
         settings.complete('success'); 
     } 
 } 

});
/code

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

.filter problem: TypeError: 'undefined' is not a function

Chris,

Thank you for the update. Let us know if you need any further help.

Return to “Issues”