I’m developing an application and I would like to use a db to store data so I don’t constantly ask it to the server, but I’m having problems with expo-sqlite, can anyone explain me what I’m doing wrong ?
i have a file database.js
`import * as SQLite from 'expo-sqlite';
const db = SQLite.openDatabase('myDatabase.db');
export const createTables = () => {db.transaction(tx => {tx.executeSql(CREATE TABLE IF NOT EXISTS users ( uid INTEGER PRIMARY KEY, picture TEXT, pversion INTEGER ));});}; then I have a viewmodel for data management where I would use the storemanager`import { useEffect, useState } from 'react';import { fetchTopUsers, userDetails } from '../api';import { useSession } from './../utils/SessionManager';import StorageManager from './../utils/StorageManager';
export const useUserListViewModel = () => {// const [userList, setUserList] = useState([]);const [isLoading, setIsLoading] = useState(true);const { sid } = useSession();const [userTopList, setUserTopList] = useState([]);
useEffect(() => {const initializeAndFetchData = async () => {try {if (sid) {const users = await fetchTopUsers(sid);StorageManager.initialize();await updateUsers(users);} else {console.log("UserVMlist sid mancante o non valido");}} catch (error) {console.error('Errore durante l'inizializzazione o il recupero dei dati:', error);} finally {setIsLoading(false);}};
initializeAndFetchData();
}, [sid]);`
this is my storagemanager
`import { createTables, insertItem, getItems, deleteItem } from './../database';
class StorageManager {
static initialize() {
createTables();
}
static addItem(value) {
return insertItem(value);
}
static fetchItems() {
return getItems();
}
static removeItem(id) {
return deleteItem(id);
}
}
export default StorageManager;`
these are my dependencies
"dependencies": { "@expo/vector-icons": "^14.0.2", "@react-native-async-storage/async-storage": "^1.23.1", "@react-native-community/masked-view": "^0.1.11", "@react-navigation/bottom-tabs": "^6.5.19", "@react-navigation/native": "^6.1.12", "@react-navigation/stack": "^6.3.23", "axios": "^1.6.8", "expo": "^51.0.5", "expo-location": "~16.5.5", "expo-sqlite": "~14.0.3", "expo-status-bar": "~1.11.1", "expo-vector-icons": "^10.0.1", "react": "18.2.0", "react-native": "^0.74.1", "react-native-gesture-handler": "~2.14.0", "react-native-maps": "^1.11.3", "react-native-reanimated": "^3.7.1", "react-native-safe-area-context": "^4.9.0", "react-native-screens": "^3.29.0" },
I expect it to work, but I have these two errors and I don’t understand how to fix it
ERROR TypeError: SQLite.openDatabase is not a function (it is undefined), js engine: hermes
ERROR Invariant Violation: "main" has not been registered. This can happen if:
Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
A module failed to load due to an error and AppRegistry.registerComponent wasn't called., js engine: hermes
Gianmarco Lobaccaro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.