Load various information retrieved from an HTTP API into a SQLite database

I am currently working on an Ionic2 project that requires users to log in before gaining access to the system. Once the login is successful, I need to post their username to another API to retrieve a list of all entries made by them. The challenge I am facing is inserting all the returned entries into an SQLite database.

userlogin(){

    let loader = this.LoadingController.create({
      content: 'Please Wait'
      });
      loader.present().then(()=>{

    this.http.post("http://localhost/app/login.php", { 'username': this.username, 'password': this.password }).map(res => res.json()) .subscribe(data => {

      if(data.message!="Incorrect Username or Password"){

     this.http.post("http://localhost/app/entries.php", { 'username': this.username}).map(res => res.json()) .subscribe(data => {
      console.log(JSON.stringify(data));

      this.sqlite.create({
        name: 'entries.db',
        location: 'default'
        })
        .then((db: SQLiteObject) => {
        //data insert section
        db.executeSql('INSERT INTO entries_table(entry) VALUES(?)', 
        [this.data.entry]).then((data)=> {

        }, (error) => {

        });


        })
     });



    }else{

    if(data.message=="Incorrect Username or Password"){
      let alert = this.alertCtrl.create({
        title: 'Error!',
        subTitle: 'Wrong Username or Password',
        buttons: ['Try Again']
      });
      alert.present();
    }
    }
    loader.dismiss();
    });

  },error=>{
    let alert = this.alertCtrl.create({
      title: 'Error!',
      subTitle: 'Please check your Internet Connectivity',
      buttons: ['Try Again']
    });
    alert.present();
  }) 
  }

The login process is working smoothly, but the challenge lies in inserting multiple data returned by the API into the SQLite database simultaneously.

Answer №1

To simplify your process, utilize Ionic Storage and configure the driver to utilize sqlite. It's a straightforward solution.

import { Storage } from '@ionic/storage';

export class MyApplication {
  constructor(private storage: Storage) { }

  ...

  // Set a key/value pair
  storage.set('entries', returnedEntryObject);

  // Retrieve a key/value pair
  storage.get('entries')
    .then((returnedEntryObject) => {
        console.log('The user entries are', returnedEntryObject);
  });
}

For more information, refer to the provided link.

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

Can you clarify the functionality of this loop? I am having trouble grasping how it produces the final result

Seeking clarification on the highlighted section] https://i.sstatic.net/dyrKS.png I need assistance in understanding how the use of "text" helps to print the following literal. ...

Utilizing React with Typescript to Implement useReducer with Action Interface Featuring Union Type Support

My current challenge involves creating a reducer using the useReducer hook. I have defined an interface named Action which includes a property that can hold either a string or a number: type Actions = 'update_foo' | 'update_bar'; inter ...

Using Java beans to present form data utilizing session

How can I utilize Java Beans and session beans to store user input data and display a preview of the entered details on the next page? I have already created a servlet using JSP, but now I want to incorporate Java Beans to showcase the form data. How shoul ...

Please enter data into the input fields provided in the text

Below is the code where Google transliteration is used for typing in Indian language in a text field. There are two routes with different page IDs. Initially, the transliteration works fine on the default page. However, when changing routes, an error occur ...

Testing reactive streams with marble diagrams and functions

Upon returning an object from the Observable, one of its properties is a function. Even after assigning an empty function and emitting the object, the expectation using toBeObservable fails due to a non-deep match. For testing purposes, I am utilizing r ...

The partial template is not functioning as anticipated

Introducing an interface designed to accept two templates, with the resulting function being a partial of one of them (similar to React-Redux): export type IState<TState, TOwnProps> = { connect: (mapStateToProps: MapStateToProps<TState, Parti ...

Using React Bootstrap to conditionally render columns within an array map

Within my React application, I am currently utilizing the map function to generate Bootstrap columns in the JSX code of the render method. One specific attribute within the array object I'm mapping is named "taken." Depending on whether this attribute ...

Ways to utilize jquery to limit the length of an editable div and prevent it from exceeding our specified restriction

Imagine a scenario where you have the following code snippet: <div id="editing" contenteditable onclick="document.execCommand('selectAll',false,null)">Put text here...</div> In this situation, let's say you want to impose a r ...

Execute JavaScript AJAX Request On Page Load

I am facing an issue with creating an onload event in which an AJAX function needs to be called and passed a value. The challenge lies in the fact that I have both my header and footer split into sections via PHP, therefore I cannot place it on the body ta ...

How can the outcome of the useQuery be integrated with the defaultValues in the useForm function?

Hey there amazing developers! I need some help with a query. When using useQuery, the imported values can be undefined which makes it tricky to apply them as defaultValues. Does anyone have a good solution for this? Maybe something like this would work. ...

Freezing objects using Object.freeze and utilizing array notation within objects

Could someone kindly explain to me how this function operates? Does [taskTypes.wind.printer_3d] serve as a method for defining an object's property? Is ["windFarm"] considered an array containing just one item? Deciphering another person& ...

The Canvas drawn using JavaScript is not being resized and painted correctly on Safari mobile and Firefox mobile browsers

I created an HTML page that features a canvas element where I am drawing a roulette using JavaScript After checking the CanIuse website, I learned that canvas functionality is supported in Firefox 68 and Safari 4 to 13 <!DOCTYPE html> <html> ...

Having difficulty changing the visibility of a div element

I am currently working on a project that involves jQuery and ASP.Net. My main goal is to create a button that can hide/show a div using jQuery. Below is the code that I have implemented: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLI ...

Refresh cookies on monitor using Vue.js

I am in the process of implementing cookies on my website. Initially, I have set up the cookies to be initialized with an empty object when the component is mounted. The goal is to update the cookie whenever the object data changes, but unfortunately, it&a ...

Implement a redux-form within a react-bootstrap modal

I am facing a challenge with incorporating a multipage 'redux-form' form into a react-bootstrap modal. My goal is to have the form displayed within the modal overlay when the modal is opened. How can this be achieved? The code below is producin ...

The issue with logging out feature

Operating an ASP.NET Web application, I have a Logout feature implemented in JavaScript. However, my current code closes the browser upon Logout, which is not the desired behavior. Instead, I am looking to clear cookies/session and redirect the user to the ...

I am disappointed with the lack of functionality in Angular's HTML type inference

When working inside an Angular component, I want to select a div element by id or class. This method works menuDisplayDiv = document.getElementsByClassName("some_class")[0] as HTMLDivElement; menuDisplayDiv = document.getElementById("some ...

"Experience the power of utilizing TypeScript with the seamless compatibility provided by the

I'm attempting to utilize jsymal.safeDump(somedata). So far, I've executed npm install --save-dev @types/js-yaml I've also configured my tsconfig file as: { "compilerOptions": { "types": [ "cypress" ...

Testing chai: verifying the inclusion of object types in an array

I am currently in the process of testing a Node.js/Typescript application. My goal is to have my function return an array consisting of objects. These objects should adhere to the following type: type myType = { title: string; description: string; ...

The MUI datagrid fails to display any rows even though there are clearly rows present

Today, I encountered an issue with the datagrid in Material UI. Despite having rows of data, they are not displaying properly on the screen. This problem is completely new to me as everything was working perfectly just yesterday. The only thing I did betwe ...