【问题标题】:IPFS is not a constructor: Nodejs - IPFS/OrbitDB chatroomIPFS 不是构造函数:Nodejs - IPFS/OrbitDB 聊天室
【发布时间】:2020-07-18 19:10:32
【问题描述】:

我每次都在尝试使用 Nodejs 和 IPFS/OrbitDB 构建 Dapp,但我尝试启动我的应用程序时出现错误:

this.node = new IPFS({ ^

TypeError:IPFS 不是构造函数

这是我没有特定 Swarm 的基本代码:

const Ipfs = require('ipfs');
const OrbitDB = require('orbit-db');

class chatroom {
    constructor(IPFS, OrbitDB) {
        this.OrbitDB = OrbitDB;
        this.node = new IPFS({
            preload: {enable: false},
            repo: "./ipfs",
            EXPERIMENTAL: {pubsub: true},
            config: {
                Bootstrap: [],
                Addresses: {Swarm: []}
            }
        });
        this.node.on("error", (e) => {throw (e)});
        this.node.on("ready", this._init.bind(this));
    }
    async _init(){
        this.orbitdb = await this.OrbitDB.createInstance(this.node);
        this.onready();
    }
}

module.exports = exports = new chatroom(Ipfs, OrbitDB);

我在以下版本的 IPFS 上运行:ipfs@0.42.0

我也在一个空的 Nodejs 应用程序上进行了尝试,当我添加一个特定的 Swarm 进行连接时,我也遇到了同样的错误。

非常感谢您的帮助,感谢您提前提供的时间。

亲切的问候

贝尼

【问题讨论】:

  • 试试 this.node = await IPFS.create()

标签: javascript node.js publish-subscribe ipfs orbitdb


【解决方案1】:

我现在就是这样做的:

const IPFS = require('ipfs');

async function createNode() {
        let node = await IPFS.create(
            {
                repo: (() => `repo-${Math.random()}`)(),
                    "Addresses": {
                        "Swarm": [
                            "/ip4/0.0.0.0/tcp/4001"
                        ],
                        "API": "/ip4/127.0.0.1/tcp/5001",
                        "Gateway": "/ip4/127.0.0.1/tcp/8080"
                    }
            }
            );
        try {
            await node.start();
            console.log('Node started!');
        } catch (error) {
            console.error('Node failed to start!', error);
        }
    }

(感谢@Eugene)

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2018-10-03
    • 2021-08-26
    • 2019-08-23
    • 2012-01-29
    • 2019-03-13
    • 2022-08-17
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多