Skip to content

Instantly share code, notes, and snippets.

@berry
Created August 4, 2016 16:11
Show Gist options
  • Save berry/493ba343f21f3b9d5e4ff3272c4b8000 to your computer and use it in GitHub Desktop.
Save berry/493ba343f21f3b9d5e4ff3272c4b8000 to your computer and use it in GitHub Desktop.
orbit db
<script type="text/javascript">
const address = 'localhost:3333';
const username = 'user1';
const password = '';
const channel = 'test'
const ipfs = new window.Ipfs();
const logger = Logger.create("orbit-db autorga", { color: Logger.Colors.Green, showTimestamp: true, showLevel: false })
const orbit = OrbitDB.connect(address, username, password, ipfs);
const putget = (key,value) => {
orbit
.then((orbit) => orbit.kvstore(channel))
.then((db) => { db.put(key, value)
.then((res) => {
const result = db.get(key)
print (result)
})
}).catch((e) => logger.error(e))
}
const put = (key,value) => {
orbit
.then((orbit) => orbit.kvstore(channel))
.then((db) => { db.put(key, value)
.then((res) => print (res))
})
.catch((e) => logger.error(e))
}
const get = (key) => {
orbit
.then((orbit) => orbit.kvstore(channel))
.then((db) => {
const result = db.get(key)
print (result)
}).catch((e) => logger.error(e))
}
const print = (res) => logger.log(res)
// 2016-08-04T15:52:45.933Z [DEBUG] orbit-db: Connecting to network localhost:3333 (localhost:3333)
// 2016-08-04T15:52:46.451Z [DEBUG] orbit-db: Connected to network localhost:3333 (localhost:3333)
// putget("a", "hellowaterworld")
// undefined
// 2016-08-04T15:53:10.270Z orbit-db autorga: hellowaterworld
// put("a", "hellowaterworld")
// undefined
// 2016-08-04T15:53:18.116Z orbit-db autorga: QmVGT4Lo8kbihYJ3S5ozXwZ2QvVJUnEiCK8H2TRvPgqNVW
// get("a")
// 2016-08-04T15:53:21.240Z orbit-db autorga: undefined
// undefined
</script>
@haadcode
Copy link

haadcode commented Aug 4, 2016

It works if the code is changed to the following. Not sure why, will have to investigate. Likely a bug, so thanks for spotting!

      const db = orbit
          .then((orbit) => orbit.kvstore(channel))

      const putget = (key,value) => {
          db.then((db) => { db.put(key, value)
            .then((res) => {
              const result = db.get(key)
              print (result)
            })
          }).catch((e) => logger.error(e))
        }

      const put = (key,value) => {
          db.then((db) => { db.put(key, value)
            .then((res) => {
              print (res)
            const result = db.get(key)
            print (result)
            })
          })
          .catch((e) => logger.error(e))
        }

      const get = (key) => {
          db.then((db) => {
            const result = db.get(key)
            print (result)
          }).catch((e) => logger.error(e))
      }

      const print = (res) => logger.log(res)

@berry
Copy link
Author

berry commented Aug 4, 2016

Thanks. That works! I hope the bug gets fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment