im trying to pass a my models service data to my action so i can change the state of all the models in the list, but im not able to pass the value of it to a variable so i can return it in my _getModelsFromBd function to pass it to my action here is the code:
this is my _getModelsFromBd code:
private _getModelsFromBd(): Modelo[] {
var res2;
this._modelsService.getModels().then(res => {
res2 = res;
console.log('res2 value inside then: ', res2);
});
console.log('res2 value outside then: ', res2)
return res2; }
and this is where i pass the value to my action:
@Effect()
getModelos$ = this._actions$.pipe(
ofType<GetModelos>(EModeloActions.GetModelos),
map(action => action.type),
withLatestFrom(this._modelsService.getModels()),
switchMap(async ([code, models]) => {
console.log("value of function: ",this._getModelsFromBd());
return of(new GetModelosSuccess(this._getModelsFromBd()));
}));
this is the output i get
res2 value outside then: undefined
value of function: undefined
res2 value inside then: (16) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Thank you for your time
I see you are calling idbHelper which is returning a promise in getModels
method. So first I would convert that to an observable by the following code:
import { from, Observable } from 'rxjs';
public getModels(): Observable<Modelo[]> {
try {
return from(IdbHelper.idbCon.select({ from: 'Modelos' })) as Observable<Modelo[]>;
} catch (error) {
throw error.message;
}
}
This would help me in converting from one(action) stream to another
and in @Effect
I would pass it as an observable only as below
@Effect()
getModelos$ = this._actions$.pipe(
ofType<GetModelos>(EModeloActions.GetModelos),
map(action => action.type),
switchMap(() => {
return this._modelsService.getModels()
.pipe(map(modelos => new GetModelosSuccess(modelos)));
})
);