WebSQL抛出错误[error: error code 1: no such table: document-stor



我们在应用程序中使用react-naive-sqlite-2RxDB,并且开始偶尔出现此错误。它似乎发生在我们删除数据库之后。我很惊讶地看到这是一个WebSQL错误,因为我们正在使用react-native和WebSQL被弃用。我没有很好的方法来调试这个,但我的直觉是,我们有一些代码仍然试图访问现在已经死亡的数据库。

这是我们用来设置数据库的代码:

import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite'
import SQLite from 'react-native-sqlite-2'
import { addRxPlugin, createRxDatabase } from 'rxdb'
import { RxDBReplicationGraphQLPlugin } from 'rxdb/plugins/replication-graphql'
import type { DatabaseType } from '../generated'
/**
* SQLITE SETUP
*/
const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
addRxPlugin(SQLiteAdapter)
addRxPlugin(require('pouchdb-adapter-http'))
/**
* Other plugins
*/
addRxPlugin(RxDBReplicationGraphQLPlugin)
export const getRxDB = async () => {
return await createRxDatabase<DatabaseType>({
name: 'gatherdatabase',
adapter: 'react-native-sqlite', // the name of your adapter
multiInstance: false,
})

问题发生在我们注销并尝试重新登录后。当我们注销时,调用removeRxDatabase。有没有人遇到过这种问题之前或知道的方法来调试?

对于后代来说,问题是我们在状态管理库(Zustand)中有一个对数据库的引用,该引用一直保留到注销之后。当我们试图再次登录时,我们的getOrCreateDatabase函数没有创建一个新的,但它无效,因为我们在rxdb中运行了database.remove()。我们最终只是清空了Zustand数据库,并在一个地方调用了database.remove()

export const useRxDB = create<UseRxDBType>((set, get) => ({
database: undefined,
/**
* we set the database to ready in the LocalDocument store once local docs are loaded into the store
*/
databaseIsReady: false,
getOrCreateDatabase: async () => {
let database = get().database
if (!database) {
database = await createRxDatabase()
if (!Rx.isRxDatabase(database)) throw new Error(`database isnt a valid RxDB database.`)
set({ database })
}
return database
},
resetDatabase: async () => {
const database = get().database
if (database) {
await database.remove()
set({ database: undefined })
}
},
}))

最新更新