简体   繁体   中英

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')

I tested the same function on my web app and works perfectly. By the way I'm using it on android physical device and an iOS emulator.

 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...

  "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.

Try replacing (await db).addCollections with await db.addCollections , like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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