just remove : != null
it will work super fine
<img src="{{foo.url}}" onerror="this.src='path/to/altimg.png'">
It worked for me.