Angular filter works, but sees input as undefined and throws error

cpk

I wrote a filter that appears to be working fine, although I am getting the following error.

TypeError: Cannot read property 'replaceAll' of undefined

The filter replaces erroneous relative urls with absolute ones. Here is the filter and markup.

//located elsewhere
String.prototype.replaceAll = function(s,r){
    return this.split(s).join(r);
}
//located elsewhere
var imgBase = '<img src="http://discourse.mysite.com';

app.filter('prependImg_topic', function(){
  return function(cooked) {
    return cooked.replaceAll('<img src="//discourse.mysite.com', imgBase);
  }
})

Markup:

<div class="topic-post-body" ng-bind-html="topic.post_stream.posts[0].cooked | prependImg_topic "></div> "

I have an inclination that I need to use $sce because I am inputting the data in ng-bind-html. If I print the value like so: {{topic.post_stream.posts[0].cooked}} , it exists, so I know it is not undefined. In addition, the filter works and properly writes urls. Go figure.

I can provide any additional details needed to help answer. Thanks.

EDIT: Another oddity that may help is that inside of a ng-repeat this filter works, and throws no errors. Trippy stuff!

<div class="post" ng-repeat="post in posts" ng-if="$index > 0">
     <div class="topic-post-body" ng-bind-html="post.cooked | prependImg_topic"></div>
</div>
jcz

I guess the data is loaded asynchronously, if that's the case it is no wonder the filter throws an errors since it is trying to operate on (yet) non-existent data. The check you make {{ topic ... }} doesn't prove anything because it will print the data if that data will eventually come (better check with console.log). The reason why the filter inside ng-repeat doesn't throw any errors is because the ng-repeat will only work if there is any data to iterate on, that means that when the filter inside ng-repeat runs the data is already there. That's also the reason why you should always construct filters in such a way that they can handle undefined or null values without throwing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Vue Global Filter throws error of value undefined

$(this) throws "undefined" but this works as intended

Angular throws an error even though my application works

Angular 2: {{object}} works, {{object.child}} throws error

Angular 2 NgFor Regex Message Error - [(ngModel)] works but ngModel is undefined

Undefined method `foo' for nil:NilClass — throws error in view but works in rails console

'$ is undefined' error but the function works

contract name throws undefined error?

Angular throws "Uncaught TypeError: Cannot read property 'charAt' of undefined" with password input

Angular app throws error

Angular ngAnimate throws error

Scan with quotes in filter throws an error

Angular App works when all components are in one file, but throws 'nomod' error when components are in separate files

Angular filter throwing "Can't interpolate" error even though the filter works

Angular @Input value is undefined

Reading input throws compile error

Using custom pipe in angular 2, unable to load the data on load of the html throws error cannot read property of undefined

Angular 6 bug in Chrome, function($event) randomly throws error "can't read property "value" of undefined"

How can I use Angular service function?It throws Error:"Cannot read property of undefined"

App Throws Fatal Error, then reboots and works

Why popFirst throws an error, but removeFirst works?

Opening file throws error but still works

HttpGet throws 500 error on vuejs but works with postman

Django csv upload to db works but throws error

JavaScript canvas throws an Error, but works anyway

Angular ng-repeat and filter error (version 1.1.5 works, 1.2.15 with error)

JQuery script works on Firefox but throws "undefined is not a function" in Chrome

Angular universal installation throws error

Angular with eslint on accessibility throws error