Parag Pathari
Parag Pathari

Reputation: 291

react native expo getting error _ExpoSQLiteNext.default.NativeDatabase is not a constructor in SQLite.openDatabaseAsync('db.testDb')

I'm getting below error when I used SQLite.openDatabaseAsync('db.testDb');

Error TypeError: _ExpoSQLiteNext.default.NativeDatabase is not a constructor

Code snippets :-

    import * as SQLite from 'expo-sqlite'

    export const DbConnection = async () => {
    const db = SQLite.openDatabaseAsync('db.testDb') // returns Database object

    // Check if the items table exists if not create it
    db.transaction(tx => {
        tx.executeSql(
            'CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT, count INT)'
        )
    });

    db.transaction((tx) => {
        tx.executeSql(
            "SELECT * FROM items",
            null,
            (txObj, resultSet) => {
                //setNoteArr(resultSet.rows._array);
                console.log(resultSet.rows._array);
            },
            (txObj, error) => console.log(error)
        );
    });
}

Can anyone encountered this error? If yes, what's the resolution?

Thank you.

Upvotes: 1

Views: 761

Answers (1)

Ivo Havener
Ivo Havener

Reputation: 51

You can either mock the db:

jest.mock('expo-sqlite');

Or for a functional use-case, try sqlite3 or similar for scripts or tests that run outside of a bundled context.

When using expo-sqlite in a non-bundled scenario, like running directly in your desktop OS, there is no concrete database associated with it. In other words, they don't set up a concrete database implementation under the expo wrapper.

So when you call openDatabase(Sync|Async) it attempts to call the native db constructor but only finds the base class that doesn't have its own constructor.

expo has its own mock implementation, but that's not loaded until you ask jest to mock the whole module.

Here's the link for that mock: github link (as of expo-sqlite v14)

Upvotes: 0

Related Questions