0

My app work fine in web, but don't when run in android studio.

problem

the next code in a component is has problem:

setup() {

const app = getCurrentInstance()

const sqlite: SQLiteHook = app?.appContext.config.globalProperties.$sqlite; // always return null

.......

}

cont app return ever null

this is my main.ts is

...
/* SQLite imports */
import { defineCustomElements as jeepSqlite, applyPolyfills } from "jeep-sqlite/loader";
import { Capacitor } from '@capacitor/core';
import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from '@capacitor-community/sqlite';

import { tablasOffile } from './utils/utils-db-no-encryption-offile';

import { useState } from './composables/state';

applyPolyfills().then(() => {
  jeepSqlite(window);
});

window.addEventListener('DOMContentLoaded', async () => {
  const platform = Capacitor.getPlatform();
  const sqlite: SQLiteConnection = new SQLiteConnection(CapacitorSQLite)

  const app = createApp(App)
      .use(IonicVue)
      .use(router)
      .use(Store)
      .use(VueAxios, axios);

  /* SQLite Global Variable   */
  const [existConn, setExistConn] = useState(false);
  app.config.globalProperties.$existingConn = {existConn: existConn, setExistConn: setExistConn};
  app.config.globalProperties.$sqlite = sqlite;

  try {
    if(platform === "web") {
      const jeepSqlite = document.createElement('jeep-sqlite');
      document.body.appendChild(jeepSqlite);
      await customElements.whenDefined('jeep-sqlite');
      await sqlite.initWebStore();
    }

    const ret = await sqlite.checkConnectionsConsistency();
    const isConn = (await sqlite.isConnection("DATABASE_NAME")).result;
    let db: SQLiteDBConnection

    if (ret.result && isConn) {
      db = await sqlite.retrieveConnection("DATABASE_NAME");
    } else {
      db = await sqlite.createConnection("DATABASE_NAME", false, "no-encryption", 1);
    }
    await db.open();


    const res = await db.execute(tablasOffile);
    if (res.changes.changes < 0) {
      console.log("Error: execute failed");
    }

    await sqlite.closeConnection("DATABASE_NAME");

    router.isReady().then(() => {
      app.mount('#app');
    });

  } catch (err) {
    console.log("Error init app: ",err);
  }
});

the database has create fine in main.ts

I take part of code from this git

---- RESOLVE ---- I resolve the problem using inject

add next line main.js

app.provide("$sqlite", sqlite);

in de components call

import {defineComponent, inject} from 'vue';

...
 setup() {
 const sqlite :SQLiteHook = inject('$sqlite');

...

}
...
5
  • Please provide you error message as text inside your answer. The Image seems to be cut so you cant see the full errormessage. Commented Aug 18, 2022 at 16:00
  • When i go to to file compiled then problemn is here a=e?.appContext.config.globalProperties.$sqlite this problemn is only in emulator android, when i use the app in browser work fine,but the i need in mobile Commented Aug 19, 2022 at 17:41
  • getCurrentInstance() is not a public API - stackoverflow.com/questions/72209080/… i just downloaded the sample vite vue project and it runs fine, I would go back to that point and start to add code until it breaks Commented Aug 19, 2022 at 21:27
  • Yes, getCurrentInstance() is not public, but in web work fine. the problem is when i make de build to test in android, this line ever return null. Know how replace this? Commented Aug 22, 2022 at 13:13
  • i solved the problem in mobil using inject Commented Aug 23, 2022 at 14:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.