简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM