简体   繁体   English

创建 collections 后无法在 react-native 上将 RxDB 与 couchdb 同步

[英]Unable to sync RxDB with couchdb on react-native after creating collections

Why would rxdb throw this error when try to connect/sync on couch server Error Sync: TypeError: undefined is not an object (evaluating 'db.todos.syncCouchDB')为什么 rxdb 在沙发服务器上尝试连接/同步时会抛出此错误Error Sync: TypeError: undefined is not an object (evaluating 'db.todos.syncCouchDB')

I tested the same function on my web app and works perfectly.我在我的 web 应用程序上测试了相同的 function 并且运行良好。 By the way I'm using it on android physical device and an iOS emulator.顺便说一句,我在 android 物理设备和 iOS 模拟器上使用它。

 try {
       //syncing...
       db.todos.syncCouchDB({
        remote: 'http://admin:somepassword@127.0.0.1:5984/itony_todo_app',
         options: {
           live: true,
           retry: true,
         },
       });
     } catch (error) {
      console.log(`Error Sync: ${error}`);
      throw error;
    }

My whole database init file is as follows:我的整个数据库初始化文件如下:

import {createRxDatabase} from 'rxdb';
import {addPouchPlugin, getRxStoragePouch} from 'rxdb/plugins/pouchdb';
import SQLite from 'react-native-sqlite-2';
import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite';
import {RxTodoDatabase, todoSchema} from './src/schema/todo';

const SQLiteAdapter = SQLiteAdapterFactory(SQLite);

addPouchPlugin(SQLiteAdapter);
addPouchPlugin(require('pouchdb-adapter-http'));

async function _create(): Promise<RxTodoDatabase> {
  let db;
  try {
    console.log(`Creating Database`);
    db = await createRxDatabase<RxTodoDatabase>({
      name: 'itony_todo_app',
      storage: getRxStoragePouch('react-native-sqlite'),
      password: 'mystupidlongpassword',
      multiInstance: false,
      ignoreDuplicate: true,
    });
  } catch (err) {
    console.log(`Error Creating database: ${err}`);
    throw err;
  }

  try {
    //adding collections
    console.log('adding collections');
    (await db).addCollections({
      todos: {
        schema: todoSchema,

      },
    });
  } catch (error) {
    console.log(`Error adding collections: ${error}`);
    throw error;
  }

  try {
    //syncing...
    db.todos.syncCouchDB({
      remote: 'http://admin:213580@127.0.0.1:5984/itony_todo_app',
      options: {
        live: true,
        retry: true,
      },
    });
  } catch (error) {
    console.log(`Error Sync: ${error}`);
    throw error;
  }

  console.log('Database initiated...');
  return db;
}

const DatabaseService = {
  DB_CREATE_PROMISE: _create(),
  get(): Promise<RxTodoDatabase> {
    return this.DB_CREATE_PROMISE;
  },
};

export default DatabaseService;

my package.json is as follows maybe I'm missing something, I don't know, maybe there's a certain package missing...我的 package.json 如下也许我遗漏了一些东西,我不知道,也许缺少某个 package...

  "dependencies": {
    "base-64": "^1.0.0",
    "events": "^3.3.0",
    "pouchdb-adapter-http": "^7.2.2",
    "pouchdb-adapter-react-native-sqlite": "^3.0.1",
    "react": "17.0.2",
    "react-native": "0.68.0",
    "react-native-get-random-values": "^1.7.2",
    "react-native-sqlite-2": "^3.5.2",
    "rxdb": "^11.6.0",
    "rxjs": "^7.5.5",
    "uuid": "^8.3.2"
  }

It's likely a race condition with async functions, addCollections isn't awaited.这可能是异步函数的竞争条件,不等待addCollections

Try replacing (await db).addCollections with await db.addCollections , like this:尝试将(await db).addCollections替换为await db.addCollections ,如下所示:

  try {
    //adding collections
    console.log('adding collections');
    await db.addCollections({
      todos: {
        schema: todoSchema,
      },
    });
  } catch (error) {
    console.log(`Error adding collections: ${error}`);
    throw error;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM