-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not possible to use catch / filter / or any other RxJs Observable methods after setItem(...) #10
Comments
Operators work fine, you just need to import each one, some are missing in your example : import 'rxjs/add/operator/filter';
import 'rxjs/add/observable/of'; But what are you trying to do ? Because your example has no sense :
|
Ok, you probably do not see such potential on this issue as I am :) If your function returns RxJs Observable, it should be possible to use all methods defined in this class (even if it makes no sense to use them). As I wrote above, even with import of "of" and "filter" it is not possible to apply filter on your observable instance (please, try it!). Of course, the standard observable can be used to filter without any problems. What I trying to do? Example:
For better example I created plunker also with get->catch (important when you want return the same type of value you subscribe) and get->filter->map (i think, filter is commonly used for stored values) Running plunker here: https://plnkr.co/edit/xXfiqb4FKjzc4mIrTJEc?p=preview Do you think using a filter to observable that returns console Error: |
I did try, or I wouldn't have closed the issue. This library just use normal Observables, there is nothing custom, so operators should work as usual. I'll do some more research tomorrow, thanks for the plunker. |
I can't reproduce your issues :
Did you try your code in a TypeScript aware editor ? There are several RxJS errors in your code, for exemple :
|
I get this error only to console during runtime. I use Visual studio code and it does not see any problem when using the catch method after getItem() (no warning, no red underline..). getItem return Observable and catch can be applied to observable, so in code, no problem. But in runtime, I get error, and only from observable, created by your getItem method. Apply catch to any other observable throw no errors. That's weird. In my original code i use Ok for filter. it was just a test if it does for some other function than catch (and I found filter). My primary problem is catch. I wrote this plunker only in browser, so I apologize for a misleading example according to using flatMap and filter. So, if you say that it works correctly, it means that you do not receive an error message in the console, like: I got this:
|
I do have the same error in console with your plunker, but when I reproduce the exact same scenario in a real app for testing, everything is OK. I think it's a configuration/loading problem, but I can't see where... |
Thanks for your effort. |
My demo app for testing is using Systemjs, and my real app is using webpack via the Angular CLI. |
Hi, tihis is full steps for minimal application using Webpack and Angular CLI returned this error.
Repro steps. Console: ng new als-test
cd als-test
npm install angular-async-local-storage --save Added to app.module.ts (only new parts of code): import { AsyncLocalStorageModule } from 'angular-async-local-storage';
imports: [
AsyncLocalStorageModule, app.component.html (full content): <button (click)="save()">Save</button> app.component.ts (new content): import { AsyncLocalStorage } from 'angular-async-local-storage';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
constructor(protected localStorage: AsyncLocalStorage) { }
// want to catch any error and return false instead of error
public save() {
let a: Observable<boolean>;
a = Observable.of(true);
a.catch(err => Observable.of(false));
a.subscribe(() => { console.log('A test is done'); },
() => { console.log('The test return error'); });
let b: Observable<boolean>; // you can see both variables are same type
b = this.localStorage.setItem('key', 'value');
b.catch(err => Observable.of(false)); // but here you can not use catch
b.subscribe(() => { console.log('Local storage observable test is done'); },
() => { console.log('Local storage observable error'); });
} Run and test. Console: A test is done
AppComponent.html:1 ERROR TypeError: b.catch is not a function
at AppComponent.webpackJsonp.../../../../../src/app/app.component.ts.AppComponent.save (app.component.ts:25)
at Object.eval [as handleEvent] (AppComponent.html:1)
at handleEvent (core.es5.js:12023)
at callWithDebugContext (core.es5.js:13493)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13081)
at dispatchEvent (core.es5.js:8615)
at core.es5.js:9226
at HTMLButtonElement.<anonymous> (platform-browser.es5.js:2651)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.es5.js:3881)
View_AppComponent_0 @ AppComponent.html:1
proxyClass @ compiler.es5.js:15134
webpackJsonp.../../../core/@angular/core.es5.js.DebugContext_.logError @ core.es5.js:13433
webpackJsonp.../../../core/@angular/core.es5.js.ErrorHandler.handleError @ core.es5.js:1080
(anonymous) @ core.es5.js:9230
(anonymous) @ platform-browser.es5.js:2651
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:425
onInvokeTask @ core.es5.js:3881
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:424
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask @ zone.js:192
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask @ zone.js:499
invokeTask @ zone.js:1540
globalZoneAwareCallback @ zone.js:1566
AppComponent.html:1 ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 0, nodeDef: {…}, elDef: {…}, elView: {…}} |
Fixed in 2.0.1. Thanks for reporting this issue, your perseverance (sorry about the first closing) and your help to reproduce the issue. 👍 |
Many thanks for the fix, everything works now. |
Hi. How can I catch errors and transform it into Observable.of(false)?
Or how to use filter or any other Observable method from setItem('local-storage-variable', variable) observable? I want to use catch, but I try filter also and always with the same result:
Part of my code:
Errors:
ERROR TypeError: this.asyncLocalStorage.setItem(...).filter is not a function
ERROR TypeError: this.asyncLocalStorage.setItem(...).catch is not a function
Versions in angular project:
"angular-async-local-storage": "^2.0.0", "rxjs": "^5.4.3"
Thank you in advance very much for your help.
The text was updated successfully, but these errors were encountered: