Page 1 of 1

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

Posted: Fri May 30, 2014 10:31 am
by Chris6743166

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!


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

Posted: Fri May 30, 2014 1:23 pm
by Kateryna Grynko

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.


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

Posted: Fri May 30, 2014 2:28 pm
by Chris6743166

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


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

Posted: Fri May 30, 2014 6:05 pm
by Evgene Karachevtsev

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".


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

Posted: Fri May 30, 2014 6:19 pm
by Chris6743166

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.


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

Posted: Fri May 30, 2014 7:15 pm
by Chris6743166

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


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

Posted: Fri May 30, 2014 7:17 pm
by Evgene Karachevtsev

Chris,

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