React Native中RxDB报Adapter未添加错误的解决方法
修复RxDB "Adapter not added" 错误
错误原因是RxDB未正确注册asyncstorage适配器,可按以下步骤修复:
1. 确认安装适配器依赖
先检查项目是否安装了对应适配器,未安装则执行:
# 使用npm npm install pouchdb-adapter-asyncstorage --save # 或使用yarn yarn add pouchdb-adapter-asyncstorage
2. 修正适配器导入与注册方式
替换代码中的require导入为ES6语法,避免default导出解析异常:
import RxDB from 'rxdb'; import schema from './ramsSchema'; // 替换原require语句 import AsyncStorageAdapter from 'pouchdb-adapter-asyncstorage'; import PouchDbHttpAdapter from 'pouchdb-adapter-http'; RxDB.plugin(AsyncStorageAdapter); RxDB.plugin(PouchDbHttpAdapter); const syncURL = 'couchDB url' //初始化DB函数不变 export async function initializeDB(dbName,password) { const db = await RxDB.create({ name: dbName.toLowerCase(), adapter: 'asyncstorage', password:'rams@1234', multiInstance: false, ignoreDuplicate: true, }); const collection = await db.collection({ name:'rams', schema, }); collection.sync({ remote: syncURL + dbName.toLowerCase() + '/', options: { live: true, retry: true, }, }); return db; }
3. 适配React Native新版本(可选)
如果你的React Native版本在0.60+,官方推荐使用@react-native-async-storage/async-storage替代旧版AsyncStorage,此时需要更换适配器:
npm install pouchdb-adapter-react-native-async-storage --save
然后修改导入代码:
import AsyncStorageAdapter from 'pouchdb-adapter-react-native-async-storage'; RxDB.plugin(AsyncStorageAdapter);
内容的提问来源于stack exchange,提问作者Ramesh R
相关产品推荐
相关产品推荐

