Can someone help me regarding the following problem in the database constructor
I get this:
Unhandled Rejection (TypeError): Illegal constructor
at
initDb();
constructor
const HomeCliente = () => {
const [db, setDb] = useState<Database | null>(null);
const runSet = (nombre:any,data:any) => {
db.set(nombre, data);
}
const runGet = async (nombre:any) => {
return await db.get({nombre});
}
useEffect(() => {
async function initDb() {
const store = new Storage();
const db = await store.create();
setDb(db);
}
initDb();
getItem("primevaCargaProveedores").then(res => {
primeraVezProveedores.current=res
})
runGet("proveedores").then(res => {
if(res!=undefined || res!=null){
arreglo=JSON.parse(res)
}
})
}, []);
}
Not sure if it helps but arent you supposed to wait until initDb() finished? Especially as u define it as an async function
1 Like
Thanks and what is better to use, the first code I posted or this one?
I have this doubt because the following code that I am going to post is not from the ionic-team
import { Storage, Drivers } from "@ionic/storage";
var storage: Storage ;
export const createStore = (name = "__mydb") => {
storage = new Storage({
name,
driverOrder: [Drivers.IndexedDB, Drivers.LocalStorage]
});
storage.create();
}
export const set = (key: string, val: any) => {
storage.set(key, val);
}
export const get = async (key: string) => {
const val = await storage.get(key);
return val;
}
export const remove = async (key: string) => {
await storage.remove(key);
}
export const clear = async () => {
await storage.clear();
}
Ionic-team code: https://github.com/ionic-team/ionic-storage
the last code: https://github.com/alanmontgomery/ionic-storage-example
Hi
I am not sure about the first code because I don’t do react.
The second part seems pretty clean to setup the database. Important that the create stuff happens before anything else.
And most actions are async in that part, so you need to be sure you are waiting for them to complete before doing something else on the database. The way you defined set, might give you headaches?
I’d try a simple app or isolated page to make it work to your liking.
1 Like
Thanks.
The way you defined set, might give you headaches?
Why would it give me headaches?
Whenever I call a set or a get, should I first call createStore? And if it has already been created, what does it always mean to call createStore?
Hi,
I think you need to call createStore only once at app launch, so in react there is likely some place where you should do that - knowing for sure it is completed before doing anything else. I know the angular way, but that won’t help you.
The get function is also async - I believe, just like set, delete and clear, so you may want to wait the get, before doing something else on the database, like a write or clear
Btw, the storage create is also async and you are not waiting for it either. Did you check the code at @Ionic/storage?
import { Storage } from ‘@ionic/storage’;
const store = new Storage();
await store.create()
1 Like