Within the x.component.ts
, I initiate the getSomething()
method from y.service.ts
. Since this method returns an observable, I subscribe to it. However, I encounter a peculiar issue where an object with 4 elements, one being an array of number arrays (number[][]), becomes empty after the subscription.
In my x.component.ts file:
this.y
.getSomething()
.pipe(
//create an observable to pass to child component
tap(data => (this.pautaFinal$ = of(data))),
//This object is used for development purposes
tap(data => (this.pautaFinal = data)),
//Convert the number[][] object into a number[][][]
//which is necessary
tap(
data =>
(this.escalaPorcionada = this.transformEscala(
data.escala.escalaArr,
10
))
)
)
.subscribe();
// Convert an array of number arrays into an array of arrays containing chunkSize number arrays.
transformEscala(escala: number[][] = [], chunkSize: number): number[][][] {
let results: number[][][] = [];
while (escala.length) {
results.push(escala.splice(0, chunkSize));
}
return results;
}
Additionally, in x.component, I tried replacing the third tap
with a map(data => data.escala.escala)
and then `.subscribe(data => this.escalaPorcionada = this.transformEscala(data, 10).
In y.service.ts:
getSomething(): Observable<Pauta> {
let admin: DatosAdmin;
let escala: Escala;
let rubrica: Rubrica;
let criterios: Criterios;
this.datosAdminAcumul$.subscribe(datos => (admin = datos));
this.escalaAcumul$.subscribe(datos => (escala = datos));
this.rubricaAcumul$.subscribe(datos => (rubrica = datos));
this.criteriosAcumul$.subscribe(datos => (criterios = datos));
let user = JSON.parse(localStorage.getItem("user"));
// Additional data to add to Pauta.
let extras = {
usuarioModificacion: user.id
};
const pautaFinal: Pauta = {
datosAdmin: admin,
criterios: criterios,
rubrica: rubrica,
escala: escala,
extras: extras
};
return of(pautaFinal);
}
The function within y.service.ts
contains multiple observables within the same service, subscribes to them, retrieves values, assigns these values to other variables, and finally encapsulates all of them inside a pautaFinal
object, which is then returned as an observable.
Attempts Made: I have inspected the array using Chrome debugger tool and confirmed that it exists before the subscription but turns empty afterward.
This is the visualization of escalaArr
within the observable this.escalaAcumul$
(from y.service)
https://i.sstatic.net/zMQ4a.jpg
This image displays the scenario post-subscription. This snapshot was taken right after the previous instance. https://i.sstatic.net/e6pKv.jpg
The object contains 4 more elements, none of which undergo any changes apart from the escalaArr
.
I am puzzled about what might be going wrong here. I've been grappling with this problem for some time now and would greatly appreciate any assistance. Thank you.