Code snipet on the readme seems wrong
bitcoinbrisbane opened this issue · comments
Getting this error when trying to use the snippet off the readme "usage" section.
import IPFS from 'ipfs-core'
import { createOrbitDB } from '@orbitdb/core'
;(async function () {
const ipfs = await IPFS.create()
const orbitdb = await createOrbitDB({ ipfs })
// Create / Open a database. Defaults to db type "events".
const db = await orbitdb.open("hello")
const address = db.address
console.log(address)
// "/orbitdb/hash"
// The above address can be used on another peer to open the same database
// Listen for updates from peers
db.events.on("update", entry => {
console.log(entry)
const all = await db.all()
console.log(all)
})
// Add an entry
const hash = await db.add("world")
console.log(hash)
// Query
for await (const record of db.iterator()) {
console.log(record)
}
await db.close()
await orbitdb.stop()
})()
don't think we are getting the same error, but I agree the code there seems wrong or need more details about how to set it up
don't think we are getting the same error, but I agree the code there seems wrong or need more details about how to set it up
Let me try again ...
I believe that this block:
// Listen for updates from peers
db.events.on("update", entry => {
console.log(entry)
const all = await db.all()
console.log(all)
})
should be something like the following. Note the added async
:
// Listen for updates from peers
db.events.on("update", async (entry) => {
console.log(entry)
const all = await db.all()
console.log(all)
})
if anyone gets this error
import IPFS from 'ipfs-core'
^^^^^^
SyntaxError: Cannot use import statement outside a module
add "type":"module" to package.json and if you get another error like this:
SyntaxError: The requested module 'ipfs-core' does not provide an export named 'default'
Use:
import * as IPFS from 'ipfs-core'
this was fixed here. #1113
Closing issue as fixed. Feel free to re-open if the issue has not been resolved.