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

this.key.read is not a function error when using random-access-memory with hello world example #160

Closed
aral opened this issue Jun 5, 2018 · 6 comments

Comments

@aral
Copy link
Contributor

aral commented Jun 5, 2018

To reproduce

const hypercore = require('hypercore')
const ram = require('random-access-memory')

const feed = hypercore((fileName) => { return ram }, {valueEncoding: 'utf-8'})

feed.append('hello')
feed.append('world', (error) => {
  if (error) throw error

  feed.get(0, console.log)
  feed.get(1, console.log)
})

The error

  this.key.read(0, 32, cb)
           ^

TypeError: this.key.read is not a function
    at Storage.openKey (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/hypercore/lib/storage.js:169:12)
    at Storage.openKey (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/hypercore/lib/storage.js:167:47)
    at Feed._open (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/hypercore/index.js:192:17)
    at open (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/hypercore/index.js:107:10)
    at run (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/thunky/index.js:19:5)
    at Feed.thunk [as _ready] (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/thunky/index.js:13:5)
    at new Feed (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/hypercore/index.js:88:8)
    at Feed (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/node_modules/hypercore/index.js:28:39)
    at Object.<anonymous> (/Users/aral/indie/net/meta/spikes/databases/hypercore-isomorphic/index.js:4:14)
    at Module._compile (module.js:652:30)

Environment

package.json dependencies:

  "dependencies": {
    "hypercore": "^6.15.0",
    "random-access-memory": "^3.0.0"
  }
@aral
Copy link
Contributor Author

aral commented Jun 5, 2018

Also seeing this with random-access-idb in the browser. To reproduce, run the following after browserifying it:

const hypercore = require('hypercore')
const randomAccessIDB = require('random-access-idb')

const storage = (typeof window === 'undefined') ? './feed' : randomAccessIDB

const feed = hypercore(storage, {valueEncoding: 'utf-8'})

feed.append('hello')
feed.append('world', (error) => {
  if (error) throw error

  feed.get(0, console.log)
  feed.get(1, console.log)
})

@mafintosh
Copy link
Contributor

You have to instantiate the instance returned. filename => ram()

@aral
Copy link
Contributor Author

aral commented Jun 5, 2018

Ah, thanks, @mafintosh – RTFM apparently doesn’t work if you can’t copy what you see properly 🤦

(I’m making progress with the IDB version. I’ll post the fixed code for that once I have it running too in case anyone else stumbles onto this and then I’ll close the issue.)

@aral
Copy link
Contributor Author

aral commented Jun 5, 2018

I updated the random-access-idb spike to the following and now I’m getting a new error (Error: No node found) when it executes the get()method in the browser. Found this IRC conversation (https://botbot.me/freenode/dat/2017-06-23/?msg=23253771&page=1) that seems relevant but couldn’t find a resolution. Apologies if I’m doing something silly again.

const hypercore = require('hypercore')
const randomAccessIDB = require('random-access-idb')

const storage = (typeof window === 'undefined') ? './feed' : function () { return randomAccessIDB('hypercore-spike')('feed') }

const feed = hypercore(storage, {
  valueEncoding: 'utf-8',
  overwrite: true
})

feed.append('hello', function (error) {
  if (error) throw error

  feed.append('world', function (error) {
    if (error) throw error

    feed.get(0, console.log) // <-- Error: No node found
    feed.get(1, console.log)
  })
})

@mafintosh
Copy link
Contributor

@aral

const browserStorage = randomAccessIDB('cool-stuff')
const feed = hypercore(filename => browserStorage(filename))

@aral
Copy link
Contributor Author

aral commented Jun 5, 2018

@mafintosh Ah, gotcha, I was thinking about it all wrong. Thank you :)

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