Orbit-db: Error running Module with IPFS Daemon

Created on 25 Aug 2018  Â·  11Comments  Â·  Source: orbitdb/orbit-db

Running into an error while attempting to run code from the usage section in the readme with the ipfs-api package:

SyntaxError: Unexpected token � in JSON at position 0
    at JSON.parse (<anonymous>)
    at OrbitDB.open (/Users/alexander/Development/orbit/node_modules/orbit-db/src/OrbitDB.js:324:27)
    at <anonymous>
const IpfsApi = require('ipfs-api');
const OrbitDB = require('orbit-db');

const ipfs = IpfsApi('localhost', '5001');
const orbitdb = new OrbitDB(ipfs);

(async () => {
    try {
        const db = await orbitdb.log('hello');
    } catch (e) {
        console.log(e);
    }
})();

My local ipfs daemon is running on port 5001. Any ideas?

Easy help wanted question

All 11 comments

I was able to replicate this, and I'm not sure how to fix it, either. Good question, @alexanderattar.

Any one got any ideas?

@alexanderattar are you running a go, js, or in-proc js daemon?

I'm running a go daemon at the moment:

Initializing daemon...
Adjusting current ulimit to 1024...
Successfully raised file descriptor limit to 1024.
Swarm listening on /ip4/10.96.94.230/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

I also tried connecting to Infura's IPFS, but got the same error:
const ipfs = new ipfsAPI('ipfs.infura.io', '5001', {protocol: 'https'})

How did you spin up your daemon, this works for me:

const DaemonFactory = require('ipfsd-ctl')
const config = {
  args: ['--enable-pubsub-experiment'],
  config: {
    Addresses: {
      API: '/ip4/127.0.0.1/tcp/5001'
    }
  }
}

// start a go daemon
DaemonFactory
  .create({ type: 'go' })
  .spawn(config, (err, ipfsd) => {
    if (err) {
      throw err
    }

    ipfsd.api.id((err, id) => {
      if (err) {
        throw err
      }

      console.log(id)
      //ipfsd.stop()
    })
  })

However, I was able to reproduce with connecting to ipfs.infura.io, so I'll start by taking a look there.

Edit: just realized based on your log you were using the go-ipfs cli

I'm fairly certain it has to do with: ipfs/js-ipfs-api#806 and thus "requires go-ipfs 0.4.17 as it allows for specifying the data encoding format when requesting object data."

@alexanderattar What version of go-ipfs are you running? and what version of js-ipfs-api are you running?

Hi @mistakia that does seem like it's likely the case. I realized I was using a pretty out of date version of go-ipfs 0.4.6 😅. I upgrade to the latest and it works now! One concern I had potentially not being able to use Infura as a remote endpoint at the moment, but I reached out to their team and they let me know that they are targeting tomorrow for deploying the upgrade to 0.4.17 so that shouldn't be an issue much longer. Thanks for the help on this @mistakia @RichardLitt

One more detail actually in case it helps you or anyone else that comes across this, I am currently using [email protected]. Cheers!

Alrighty. Reinstalling ipfs can be annoying. I can no longer replicate using v0.4.17. Good.

@alexanderattar were you able to use infura as a remote endpoint for orbit-db?

@KevinLiLu Yes I was. You have to set the replicate option to false because Infura doesn't have the experimental IPFS pub/sub feature enabled. Here's some code:

```
const ipfsApi = require('ipfs-api');
const OrbitDB = require('orbit-db');

// Local for testing
// const ipfs = ipfsApi('localhost', '5001');
const ipfs = new ipfsApi('ipfs.infura.io', '5001', {protocol: 'https'})

const orbitdb = new OrbitDB(ipfs);

(async () => {
try {
// Create / Open a database
const db = await orbitdb.log('hello', {replicate: false});
await db.load()

    // Listen for updates from peers
    db.events.on('replicated', (address) => {
        console.log(db.iterator({ limit: -1 }).collect())
    })

    console.log(db.address.toString())

    // Add an entry
    const hash = await db.add('world')
    console.log(hash)

    // Query
    const result = db.iterator({ limit: -1 }).collect()
    console.log(JSON.stringify(result, null, 2))

} catch (e) {
    console.log(e);
}

})()```

@alexanderattar Got it, thanks! 😄

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huhsame picture huhsame  Â·  4Comments

whyrusleeping picture whyrusleeping  Â·  3Comments

MirceaKitsune picture MirceaKitsune  Â·  3Comments

maroodb picture maroodb  Â·  4Comments

maxkerp picture maxkerp  Â·  6Comments