Search code examples
reactjssqlitecordovaionic-frameworkcapacitor

How to use cordova sqlite plugin with Ionic 5 app in React?


I have followed the documentation here, installed cordova-sqlite-storage, @ionic-native/sqlite, and @ionic-core, and have the following code on app.tsx.

import { SQLite, SQLiteObject } from '@ionic-native/sqlite/';

function AppSession(props:any){
...


useEffect(()=>{

  SQLite.create({
    name: 'data.db',
    location: 'default'
  }).then((result) => {
    console.log("DONE");
    console.log(result);
  }).catch((err) => {
    console.log("ERROR");
    console.log(err);
  });
},[])

After that I run the app on the browser with the command ionic serve but I am getting the error Cannot read property 'then' of undefined for SQLite.create(...).

I haven't found much on the documentation or the forums. What am I doing wrong? What is the right way to use the plugin?


Solution

  • The problem is probably that you're running the app in the browser. If you build it for a device, I imagine it would work. Have a look at this repo to see a working example of a simple app using the Cordova SQLite plugin with Ionic React and Capacitor.