pouchdb-community / socket-pouch

PouchDB and CouchDB over WebSockets, using Engine.io
Apache License 2.0
207 stars 45 forks source link

Cannot read property 'on' of undefined #32

Closed vinz243 closed 6 years ago

vinz243 commented 6 years ago

I'm trying to use socket-pouch in my project, but I get this error in my console:

{
   "error":"bad_request",
   "name":"bad_request",
   "reason":"Cannot read property 'on' of undefined",
   "message":"Cannot read property 'on' of undefined",
   "status":500,
   "result":{
      "ok":false,
      "start_time":"2018-02-01T12:47:40.405Z",
      "docs_read":0,
      "docs_written":0,
      "doc_write_failures":0,
      "errors":[

      ],
      "status":"aborting",
      "end_time":"2018-02-01T12:47:40.645Z",
      "last_seq":0
   }
}

The error is from the backend since I can find it in the WS frames. Here is my backend code:

const socketPouchServer = require('socket-pouch/server');
socketPouchServer.listen(9001, {
  pouchCreator: async function (token: string) {
    // ..... validate token and generate database user pass asynchronously
    const url = `http://${user}:${pass}@${PouchDB.host}/${db}`
    const database = new pouch(url);
    return database;
  }
}, function () {
  // server started
});

Here is my frontend

const getDatabase = async function<T> (name: string) {
  const res = await session.fetch('/api/database/'+ name);
  const {token, ok} = await res.json();
  console.log(token, ok);
  return new PouchDB<T>(token, {
    adapter: 'socket',
    url: 'ws://localhost:9001'
  } as any);
}

    const dbName = dbs[0];
    const db = new PouchDB(dbName);
    const remote = await getDatabase(dbName);

    db.replicate.from(remote).on('complete', (info) => {
      ...
    }).on('error', (err: any) => {
      console.log(err);
      Toaster.error(`An error happened during database sync for ${dbName}: ${err.code}`);
    });

Versions:

npm ls pouchdb
+-- pouchdb@6.3.4
`-- socket-pouch@2.0.0 (github:compactd/socket-pouch#242dfebe2ab64a6d33266043e05dadb33e92f8f7)
  `-- pouchdb@6.3.4  deduped

EDIT: I added {pouch} and now it works, but now it is aborting on its own without any error (the error thrown is empty)

vinz243 commented 6 years ago

Looks like editing shared/utils to use default promise constructor fixed it

liederivative commented 6 years ago

Could you be a little more specific on what you did ? I mean , I just commented out 'pouch-promise' line and still having the same error.

vinz243 commented 6 years ago

Sorry I dont recall now, although here is the commit that followed https://github.com/compactd/compactd/commit/40a1a1407805cd2ba79d2465c32a5a55acd91887