Abdiel49
Abdiel49

Reputation: 1

PouchDB sync Error: {"name":"Error","message":"Can't find variable: btoa"}

Hello I have been implementing an off-line database with PouchDB in React Native 0.68.2, although when saving and retrieving the data it works correctly, but when synchronizing it throws this error:

{"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 file is:

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

shim();

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

pouchDB.ts file have:

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 runs in a docker compose container with the service as follows:

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

I have based on this tutorial: react-native-in-2022-24ej

const testSyncDB = async () => {
    const data = {
      userInfo: {
        name: 'user',
        lastName: 'nameExample',
        email: '[email protected]',
      },
    };
    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"}
  };

Upvotes: 0

Views: 84

Answers (1)

Rokoala
Rokoala

Reputation: 461

Did you called the shim.ts in your index/main file? I think that the variable was not added to global variables as the repo says:

shim()
Adds btoa and atob functions to global.

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

Upvotes: 0

Related Questions