React Native PouchDB同步错误:{“名称”:“错误”,“消息”:“找不到变量:btoa”}

8zzbczxx  于 2023-03-03  发布在  React
关注(0)|答案(1)|浏览(114)

你好我一直在React Native 0.68.2中使用PouchDB实现一个离线数据库,虽然在保存和检索数据时它工作正常,但在同步时它抛出此错误:

{"name":"Error","message":"Can't find variable: btoa"}
import PouchBD from './pouchDB';

export const syncPouchTest = async (nameDB: string) => {
  const db = new PouchBD(nameDB, {
    adapter: 'react-native-sqlite',
  });
  try {
    const sync = await PouchBD.sync(nameDB, POUCHDB_REMOTE_SERVER); // POUCHDB_REMOTE_SERVER = 'http://localhost:PORT/
    console.log('response on on sync data', sync);
    return sync;
  } catch (error) {
    const message = `error on sync pouchDB to ${POUCHDB_REMOTE_SERVER}/${nameDB}\n   === with error: \n${error}`;
    console.error(message);
    // throw error;
  }
};

shim.ts文件为:

import {shim} from 'react-native-quick-base64';

shim();

// Avoid using node dependent modules
process.browser = true;

pouchDB.ts文件具有:

import 'react-native-get-random-values';
import PouchDB from 'pouchdb-core';
import HttpPouch from 'pouchdb-adapter-http';
import replication from 'pouchdb-replication';
import mapreduce from 'pouchdb-mapreduce';
import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite';
import WebSQLite from 'react-native-quick-websql';

const SQLiteAdapter = SQLiteAdapterFactory(WebSQLite);

export default PouchDB.plugin(HttpPouch)
  .plugin(replication)
  .plugin(mapreduce)
  .plugin(SQLiteAdapter);

CouchDB在Docker合成容器中运行,服务如下所示:

version: "3.8"

services:

  ...

  couchserver:
    image: couchdb:3.2.2
    restart: always
    ports:
      - "${COUCHDB_PORT}:5984"
    environment:
      - COUCHDB_USER=${COUCHDB_USER}
      - COUCHDB_PASSWORD=${COUCHDB_PASSWORD}
    volumes:
        - ./couchdb:/opt/couchdb/data
        - ./couchdbData:/opt/couchdb/etc/local.d

我已经根据本教程:react-native-in-2022-24ej

const testSyncDB = async () => {
    const data = {
      userInfo: {
        name: 'user',
        lastName: 'nameExample',
        email: 'user@example.com',
      },
    };
    await testPouchDBService('test', data);
    // response  {"id": "89d2b549-b734-4862-adc9-e2356b17f448", "ok": true, "rev": "1-620088e18e9bccd447890a2977d2a7ed"}

    await syncPouchTest('test');
    // response: ERROR: {"name":"Error","message":"Can't find variable: btoa"}
  };
fdx2calv

fdx2calv1#

你在索引/主文件中调用了shim.ts吗?我认为变量没有像repo说的那样被添加到全局变量中:

shim()
Adds btoa and atob functions to global.

https://github.com/craftzdog/react-native-quick-base64

相关问题