TypeError: IPFS is not a constructor
TypeError: IPFS is not a constructor
我正在尝试使用 Orbit-DB,所以我按照 the guide. But at the Create a database step,我得到一个错误:
const ipfs = new IPFS()
^
TypeError: IPFS is not a constructor
这是我的完整代码:
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
// Create IPFS instance
const ipfs = new IPFS()
ipfs.on('ready', async () => {
const orbitdb = await OrbitDB.createInstance(ipfs)
const db = await orbitdb.docs('opews-db-test1')
const address = db.address
})
我检查了 require()
和 console.log()
没有错误,但似乎没有。所以我不知道如何解决...
IPFS 改变了你构建 IPFS 节点的方式,你可以试试这个代码:
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
async function main() {
const ipfs = await IPFS.create();
const orbitdb = await OrbitDB.createInstance(ipfs);
const db = await orbitdb.docs('opews-db-test1');
const address = db.address;
}
main();
一个完整的例子:
<!doctype html>
<html>
<head>
<title>IPFS in the Browser</title>
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/orbit-db/dist/orbitdb.js"></script>
<script type="text/javascript">
(async function main()
{
node = await Ipfs.create({
repo: "ipfs/shared",
config: {"Bootstrap": ["/ipv4/127.0.0.1/tcp/4002/ws/ipfs/<your go-ipfs peer id>"]},
EXPERIMENTAL: {pubsub: true }});
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
orbit = await OrbitDB.createInstance(node);
docs = await orbit.docs('my-docs', { indexBy: 'name' })
hash = await docs.put({name: 'shamb0t', followers: 500 })
console.log(hash)
console.log(docs.get('shamb0t')[0].followers)
feed = await orbit.feed('my-feed');
hash = await feed.add('hello');
console.log(hash);
console.log(feed.iterator({ limit: -1 }).collect()[0].payload.value)
//feed.del(hash)
})()
</script>
</head>
<body>
<h1 id="status">Node status: offline</h1>
</body>
</html>
我正在尝试使用 Orbit-DB,所以我按照 the guide. But at the Create a database step,我得到一个错误:
const ipfs = new IPFS()
^
TypeError: IPFS is not a constructor
这是我的完整代码:
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
// Create IPFS instance
const ipfs = new IPFS()
ipfs.on('ready', async () => {
const orbitdb = await OrbitDB.createInstance(ipfs)
const db = await orbitdb.docs('opews-db-test1')
const address = db.address
})
我检查了 require()
和 console.log()
没有错误,但似乎没有。所以我不知道如何解决...
IPFS 改变了你构建 IPFS 节点的方式,你可以试试这个代码:
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
async function main() {
const ipfs = await IPFS.create();
const orbitdb = await OrbitDB.createInstance(ipfs);
const db = await orbitdb.docs('opews-db-test1');
const address = db.address;
}
main();
一个完整的例子:
<!doctype html>
<html>
<head>
<title>IPFS in the Browser</title>
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/orbit-db/dist/orbitdb.js"></script>
<script type="text/javascript">
(async function main()
{
node = await Ipfs.create({
repo: "ipfs/shared",
config: {"Bootstrap": ["/ipv4/127.0.0.1/tcp/4002/ws/ipfs/<your go-ipfs peer id>"]},
EXPERIMENTAL: {pubsub: true }});
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
orbit = await OrbitDB.createInstance(node);
docs = await orbit.docs('my-docs', { indexBy: 'name' })
hash = await docs.put({name: 'shamb0t', followers: 500 })
console.log(hash)
console.log(docs.get('shamb0t')[0].followers)
feed = await orbit.feed('my-feed');
hash = await feed.add('hello');
console.log(hash);
console.log(feed.iterator({ limit: -1 }).collect()[0].payload.value)
//feed.del(hash)
})()
</script>
</head>
<body>
<h1 id="status">Node status: offline</h1>
</body>
</html>