首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: IPFS不是构造函数

TypeError: IPFS不是构造函数
EN

Stack Overflow用户
提问于 2020-05-13 08:43:57
回答 2查看 1.7K关注 0票数 2

我试图使用轨道数据库,所以我遵循导游.但是在https://github.com/orbitdb/orbit-db/blob/master/GUIDE.md#create-a-database,我得到了一个错误:

代码语言:javascript
复制
const ipfs = new IPFS()
             ^

TypeError: IPFS is not a constructor

这是我的完整代码:

代码语言:javascript
复制
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()中没有错误,但似乎没有。所以我不知道怎么解决..。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-13 09:06:24

IPFS已经改变了构建IPFS节点的方式,您可以试试以下代码:

代码语言:javascript
复制
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();
票数 2
EN

Stack Overflow用户

发布于 2022-03-21 06:53:45

一个完整的例子:

代码语言:javascript
复制
<!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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61770044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档