0
I have the data stored in Storage as it is in the first image and in the second image the code that should assign the Storage values to the Prods variable. This code is in a method that I will call at the click of a button. But after I call the method and print the value of the variable Prods, it is as Undefined. :(
Button code, where the method is called (add or update) depending on the situation.
<ion-button expand="block" icon-start (click)='add_or_update()'>
<ng-template *ngIf="product.id;then btn_update; else btn_add"></ng-template>
<ng-template #btn_update>
<ion-icon name="create"></ion-icon>
Atualizar
</ng-template>
<ng-template #btn_add>
<ion-icon name="add"></ion-icon>
Adicionar
</ng-template>
</ion-button>
Method that checks which action to perform (add or update)
add_or_update() {
this.product.id ? this.update() : this.add();
}
Method add that will call the method that contains the problem code
add() {
this.loadingService.present();
this.product.profile = this.auth.user.profile_id;
this.checkProductTagStorage(); // #### AQUI ####
this.productService.add(this.product).subscribe(
data => {
this.loadingService.dismiss();
this.toastService.success('Animal cadastrado com sucesso!');
this.products.unshift(this.product);
this.clear();
},
error => {
this.loadingService.dismiss();
this.toastService.error('Erro ao cadastrar produto!');
}
);
}
Method in which the problem occurs
checkProductTagStorage() {
this.prods = [];
this.storage.get('coovita-prods-list-type-1').then((data) => {
this.prods = data;
console.log(this.prods); // Imprimindo a variavel está com os dados corretos
});
console.log(this.prods); // Imprimindo aqui a variavel está vazia []
}
Would it be possible to provide more details to reproduce the problem? Maybe setting an example in stackblitz will help other people better understand the problem.
– Marcelo Vismari
I get a list of products from an API and then store in the Storage. Then I want to pass this data from the Storage to a variable. The strange thing is that inside this get the image if I have the Prods variable printed will print the values correctly. However, even after calling the method that makes this request, if I have the variable printed it is as Undefined. What would be really interesting I show to help?
– Divino Rodrigues
It would be nice to post the snippet of your template where the variable
prods
is used and the button activating the functionget
. From what I’ve seen so far it’s all right! after calling theget
the variableprods
is completed and may be used in other parts of the code.– Marcelo Vismari
I don’t use the variable in the template, I want this data just to check if there is already a registered tag with the same value. I edited the description and added the methods. I don’t understand why the data is being correctly received only within the get function. :(
– Divino Rodrigues
Now it’s clearer. I’ll leave the answer with the details. I hope it helps :)
– Marcelo Vismari