Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments not working in electron renderer with IndexedDb adapter #1371

Closed
rixo opened this issue Aug 9, 2019 · 4 comments
Closed

Attachments not working in electron renderer with IndexedDb adapter #1371

rixo opened this issue Aug 9, 2019 · 4 comments

Comments

@rixo
Copy link

rixo commented Aug 9, 2019

Case

bug

Issue

I've got an error when trying to read an attachment in electron renderer process, with IndexedDB adapter.

await attachment.getStringData()

Throws:

TypeError: Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.
    at .../node_modules/rxdb/dist/lib/plugins/attachments.js:88

Versions (from process.versions):

chrome: "73.0.3683.121"
electron: "5.0.6"
node: "12.0.0"

I managed to get it working for me by commenting out this if block. But then, I got a new error, still related to attachments, when trying to import a database dump with importDump.

Info

  • Environment: electron
  • Adapter: IndexedDB
  • Stack: Svelte

Code

In the following code, the commented line will throw when run in electron renderer thread.

The same code runs fine in a real Chrome.

The same code runs fine in electron with memory adapter.

I've put the code in a minimal electron app for reproduction: https://github.com/rixo/test-rxdb-electron-attachments. Run npm ci && npm start to test in electron.

const assert = require('assert')
const { create, plugin } = require('rxdb')

plugin(require(`pouchdb-adapter-idb`))

const db = await create({
  name: 'test_' + String(Math.random()).substr(2),
  adapter: 'idb',
  adapter: 'memory',
  adapter,
})

assert(!!db, 'db created')

await db.collection({
  name: 'foo',
  schema: {
    version: 0,
    properties: {
      name: {
        type: 'string',
      },
    },
    attachments: {},
  },
})

// create test doc
{
  const doc = await db.foo.insert({
    name: 'bar',
  })
  await doc.putAttachment({
    id: 'attached',
    data: 'Lorem ipsum dolor sit amet',
    type: 'text/plain',
  })
}

// test attachments
{
  const doc = await db.foo.findOne({ name: 'bar' }).exec()
  assert(doc.name === 'bar', 'record loaded')

  const attachments = doc.allAttachments()
  assert(attachments.length === 1)

  const attachment = await doc.getAttachment('attached')
  const value = await attachment.getStringData() // <= THROWS in electron + idb
  assert(value === 'Lorem ipsum dolor sit amet')
}

console.log(`${adapter}: OK`)
@pubkey
Copy link
Owner

pubkey commented Aug 10, 2019

I think we should start with updating the electron example. Then this problem can be reproduced there and be fixed.

@rixo
Copy link
Author

rixo commented Aug 10, 2019

Ah, yes. Sorry, I missed the electron example somehow. I'll see if I can reproduce it there.

@pubkey
Copy link
Owner

pubkey commented Aug 17, 2019

Electron has an Uint8Array instead of a blob. I added a check for this which will transform it to a blob in that case.

Please check if that fixes your problem and open another issue for the problem with importDump if necessary.

@rixo
Copy link
Author

rixo commented Aug 18, 2019

Thanks. The fix works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants