Retrieve and showcase data using Ionic 2's local storage functionality

Hi, I'm having trouble accessing my data in local storage. Every time I try, it gives me an error. I need assistance with displaying my data at home. Thank you for your help :)

Error:

Typescript Error Argument of type 'Promise' is not assignable to parameter of type 'string'.

This.user = JSON.parse(this.storage.get(this.key)); prompt.present();

Typescript

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController, AlertController } from 'ionic-angular';
import {Storage} from '@ionic/storage';

/**
 * Generated class for the CrudPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-crud',
  templateUrl: 'crud.html',
})
export class CrudPage {

  user: any = [] ;
  key: any;

  constructor(public navCtrl: NavController,
  public navParams: NavParams,
  public viewCtrl: ViewController,
  public alertCtrl: AlertController,
  public storage: Storage) {
    this.storage.forEach( (value) => {
      this.user.push(value);
    });
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CrudPage');
  }

  add() {
    let prompt = this.alertCtrl.create({
      title: 'Add User',
      message: "Enter information of the user",
      inputs: [
        {
          name: 'name',
          placeholder: 'name'
        },
        {
          name: 'password',
          placeholder: 'password'
        },
      ],
      buttons: [
        {
          text: 'Cancel',
          handler: data => {
            console.log('Cancel clicked!');
          }
        },
        {
          text: 'Save',
          handler: data => {
            let key = data.name + data.password;
            this.storage.set(key, JSON.stringify(data));
            console.log(data);
          }
        }
      ]
    });
    this.user = JSON.parse(this.storage.get(this.key));
    prompt.present();
  }

  delete(key){
    this.storage.remove(key);
  }

  update(key){

  }

}

HTML

<!--
  Generated template for the CrudPage page.

  See http://ionicframework.com/docs/components/#navigation for more info on
  Ionic pages and navigation.
-->
<ion-header>

  <ion-navbar>
    <ion-title>Crud</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
    <button ion-button clear icon-start color="dark" (click)="add()">
        <ion-icon name="add-circle">Add User</ion-icon>
    </button>

    <br>

  <ion-grid text-center>
    <ion-row>
      <ion-col width-100>
        USERS
      </ion-col>
    </ion-row>
    <ion-row>
      <ion-col width-33>
        <strong>User Name</strong>
      </ion-col>
      <ion-col width-33>
        <strong>Password</strong>
      </ion-col>
      <ion-col width-33>
        <strong>Action</strong>
      </ion-col>
    </ion-row>
    <ion-row *ngFor="let users of user" text-center>
      <ion-col width-33>
        <p>{{users.name}}</p>
      </ion-col>
      <ion-col width-33>
        <p>{{users.password}}</p>
      </ion-col>
      <ion-col width-33>
        <button ion-button clear icon-start color="dark" (click)="delete(users.name+users.password)">
          <ion-icon name="trash"></ion-icon>
        </button>
        <button ion-button clear icon-start color="dark" (click)="update(users.name+users.password)">
          <ion-icon name="create"></ion-icon>
        </button>
      </ion-col>
    </ion-row>
  </ion-grid>

</ion-content>

Please provide your assistance. Thanks a lot :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Could you please explain the specific distinctions between pipe and map within Angular 7?

After extensive research, I'm still struggling to understand the distinction between pipe and map in Angular 7. Should we always include a pipe in Service.ts file in Angular 7? Appreciate any clarification on this matter. ...

Can a custom type guard be created to check if an array is empty?

There are various methods for creating a type guard to ensure that an array is not empty. An example of this can be found here, which works well when using noUncheckedIndexedAccess: type Indices<L extends number, T extends number[] = []> = T["le ...

Iterate through JSON data and access values based on keys using a $.each loop

I have retrieved JSON data from the controller using AJAX and now I want to access this data. The data is in the form of a list of objects (array) with key-value pairs, so I am planning to use .each() function to go through all the data. The array looks li ...

What is the best way to retrieve all values in a PHP array?

Struggling to generate multiple new pages using a template where placeholders are replaced with values from arrays? It seems like the page creation is successful for a specific array index, but the goal is to create pages for all values in the arrays and n ...

Searching for an element within an array using Mongoose

I am currently facing a challenge in finding all documents with a specific value in an array field using Mongoose and Node.js. While I can achieve this task easily in MongoDB, I seem to be encountering difficulties with Mongoose implementation. Following t ...

Is there a quicker way to locate information by using keyword search?

I am currently facing the task of searching for every occurrence of "Parameters Table" within a string array, and then extracting another string from a specific index between each instance. The approach I have been using is as follows: public List<stri ...

Koffi organized a collection of structured arrays

I am currently using koffi 2.4.2 in a node.js application from koffi.dev and up until now, everything has been running smoothly. However, I have encountered an issue with integrating a native C++ library method that requires a parameter struct defined as f ...

Challenges arise when working with Angular using ngrx and typescript, particularly when dealing

I'm encountering an issue while working with ngrx. I keep receiving an error when attempting to implement a simple effect. The specific error message mentions that the Argument of type 'MonoTypeOperatorFunction<LoadContainerAction>' is ...

A guide to strictly defining the subclass type of object values in TypeScript

How do I enforce strict subclass typing for object values in the SHAPE_PARAMS definition? When using type assertion like <CircleParameter>, missing properties are not caught by linting. Is there a way to define subclass types strictly? const Shapes ...

What is the best way to use jq to directly change a JSON string into its parsed form?

I have a JSON array with multiple objects that share the same keys. However, some of these keys contain serialized JSON data instead of actual JSON format. Here is an example: [ { "a": 1, "b": "{\"c\": ...

Guide on combining two distinct JSONArray and displaying them in a single ListView

Seeking assistance with a problem I'm facing - I have received a JSON file from my server and for now, I have created two separate views, XMLs, getters & setters, and adapters following a tutorial on Youtube. However, I am still in the learning phase ...

Utilize your custom types within an npm package without the need to release them to @types

I recently came across this npm package (https://www.npmjs.com/package/react-web-notification) and decided to integrate it into my project. To do so, I created an index.d.ts file within the node_modules/@types/react-web-notification folder: import * as Re ...

Looking for the location of the traceResolution output?

When I enable traceResolution in my tsconfig.json file, where can I expect to see the resulting output? { "compilerOptions": { "traceResolution": true, ... The --traceResolution flag enables reporting of module resolution log messages. You ...

The index specified is out of bounds for the current __NSArrayM

Apologies for the redundancy, as I know similar questions have been asked multiple times. I have recently taken over management of an iOS app written in Objective C and I need to make some revisions to it. Initially, the app functioned properly in an olde ...

Error: Module './App' not found in webpack module

I am encountering the error Uncaught Error: Module not found: Can't resolve ./App' and ./store in client/src. in the console of my local environment when I execute npm start from the client directory. The console is showing 2 errors: ERROR in ...

What is preventing the value from changing in auth.guard?

I am encountering an issue with the variable loggined, which I modify using the logTog() method. When I call this method, a request is made to a service where the current result is passed to auth.guard. However, in the console, it displays "undefined". Can ...

Creating variables in Typescript

I'm puzzled by the variable declaration within an Angular component and I'd like to understand why we declare it in the following way: export class AppComponent { serverElements = []; newServerName = ''; newServerContent = &apos ...

The (functionName) does not exist within the subclass as a valid function

I am currently developing an extension for a web-based text editor. However, I am facing some unexpected results due to the class hierarchy in my code. Despite attempting to relocate the "validate" function to the base class, I have not been successful in ...

Determine the item in a collection of objects that contains a specific key

What is the most efficient method for locating an object by a specific key in JS when given an array of objects? Utilizing jQuery and underscoreJS is acceptable. I am simply seeking the simplest solution with minimal code. Illustration: Suppose we have a ...

Tips for successfully setting up a Python script to extract data from .txt files and calculate the total number of positive values

I am dealing with a Python script that is supposed to analyze a directory containing .txt files and determine whether each file returns positive or negative for specific text statements like "known infection source" within the file itself. Unfortunately, m ...