Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How to make sure my database object is created and available before anything else?

I'm using rxdb to store my data and would like to use databaseController.ts which creates the database object and export query methods which the React components can use. To achieve that I must assure that the database object has been created before anything else. Because right now I get the error TypeError: Cannot read properties of undefined (reading 'contracts') if I try to use getAllNewContractsQuery(). How can I make sure that my database object is available?

databaseController.ts

/* omitted imports etc. */

let db;

export async function initDB() {

  await createDatabase();
    await db.addCollections({
      contracts: {
        schema: contractsSchema,
        methods: contractDocMethods,
        statics: contractsCollectionMethods,
      },
    });

  return db;
}

async function createDatabase() {

  db = await createRxDatabase<DatabaseCollections>({
    name: 'contractsdb',
    storage: getRxStoragePouch('idb'),
    multiInstance: true,
    ignoreDuplicate: false
  });

}

export async function getAllNewContractsQuery() {
  const query = await db.contracts.find().where('customerName').eq('Luke Cage')
  console.log(await query.exec())
}

export { db }

App.tsx

/* omitted imports etc. */

export default function App() {

  useEffect(() => {
    const initDBEffect = async () => {
        await initDB()
    }
    initDBEffect()

  }, [])

  return (
    <Provider store={store}>
      <Router>
        <Routes>
          <Route path="/" element={<Layout />}>
            <Route index element={<ContractManager />} />
          </Route>
        </Routes>
      </Router>
    </Provider>
  );
}

contractManager.tsx

const ContractManager = () => {
  
  useEffect(() => {
    const newContracts = async () => {
      await getAllNewContractsQuery()
    }

    newContracts()
  }, [])

  /* omitted code */
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda