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');

...

}
...
Francisco
  • 16
  • 3
  • Please provide you error message as text inside your answer. The Image seems to be cut so you cant see the full errormessage. – Lalaluka Aug 18 '22 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 – Francisco Aug 19 '22 at 17:41
  • getCurrentInstance() is not a public API - https://stackoverflow.com/questions/72209080/vue-3-is-getcurrentinstance-deprecated?rq=1 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 – Aaron Saunders Aug 19 '22 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? – Francisco Aug 22 '22 at 13:13
  • i solved the problem in mobil using inject – Francisco Aug 23 '22 at 14:57

0 Answers0