craftzdog / pouchdb-adapter-react-native-sqlite

PouchDB adapter using ReactNative SQLite as its backing store
https://github.com/craftzdog/pouchdb-react-native
MIT License
122 stars 21 forks source link

Map/Reduce - ReferenceError: Property 'bytecode' doesn't exist #25

Open aharish opened 1 month ago

aharish commented 1 month ago

Hello Takuya! Thanks so much for the library, and for adding a fast backend for PouchDB on React Native!

I have been using the v4 branch since you pushed it out a few weeks ago. It mostly works fine, but I am running into issues with using map/reduce queries.

Just running a map query by itself works fine, either with an indexed view, or as a temporary query. However, when running it with a reduce function, this error gets thrown.

ReferenceError: Property 'bytecode' doesn't exist
    at reduce (JavaScript:1:80)
    at tryReduce (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:189429:22)
    at reduceView (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:190052:34)
    at ?anon_0_ (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:190125:40)
    at next (native)
    at asyncGeneratorStep (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:5852:19)
    at _next (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:5866:29)
    at anonymous (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:5871:14)
    at tryCallTwo (address at InternalBytecode.js:1:1222)
    at doResolve (address at InternalBytecode.js:1:2541)
    at Promise (address at InternalBytecode.js:1:1318)
    at anonymous (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:5863:25)
    at apply (native)
    at _onMapResultsReady (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:190167:42)
    at apply (native)
    at onMapResultsReady (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:190119:42)
    at ?anon_0_ (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:190236:35)
    at next (native)
    at asyncGeneratorStep (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:5852:19)
    at _next (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:5866:29)
    at tryCallOne (address at InternalBytecode.js:1:1180)
    at anonymous (address at InternalBytecode.js:1:1874)
    at apply (native)
    at anonymous (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:14848:26)
    at _callTimer (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:14727:17)
    at _callReactNativeMicrotasksPass (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:14772:17)
    at callReactNativeMicrotasks (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:14978:44)
    at __callReactNativeMicrotasks (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:2637:48)
    at anonymous (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:2410:45)
    at __guard (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:2609:15)
    at flushedQueue (http://192.168.10.157:8081/node_modules/expo-router/entry.bundle//&platform=android&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=app:2409:21)

My project is setup similar to the one in the example directory of the v4 branch.

Here is the map/reduce function I am calling via db.query:

db.query({
  map: function (doc, emit) {
    if(doc.type == "task" && doc.project_id) {
      emit(doc.project_id, {type: "task", status: doc.status});
    }
  },
  reduce: function (keys, values, rereduce) {
    if (rereduce) {
      return values;
    } else {
      var total = 0;
      var complete = 0;
      for(var i = 0; i < values.length; i++) {
        if(values[i].type == "task") {
          if(values[i].status == "D" || values[i].status == "C") {
            complete = complete + 1;
          }
          total = total + 1;
        }
      }
      return {total: total, complete: complete};
    }
  }
}

The above query has been tested in a React app with PouchDB and also in a CouchDB server and does work without any issues.

Please let me know if there is any further logs I can provide.

Reproduction should be possible with any map/reduce query as far as I am aware. I have a couple other simpler reduce queries that also throw the same error.

craftzdog commented 1 month ago

I'm planning to publish a new article on how to properly use the new adapter once I've finished testing it on my app.

aharish commented 1 month ago

Awesome, looking forward to it!

craftzdog commented 2 weeks ago

check out the guide: https://www.devas.life/the-fastest-way-to-use-pouchdb-on-react-native-0-73/