node.js ipfs发送消息typeerror:无法读取未定义的属性(Reading' Prublish Qupprance')

发布于 2025-01-22 13:10:10 字数 1597 浏览 1 评论 0原文

我正在尝试通过IPFS网络发送消息,但我一直在获得错误typeError:无法读取未定义的属性(读取'prublish')

错误发生在line node.pubsub.publish(topic,msg,(err)=> {})

ipfs.js是应用程序的主引擎,并包含与IPFS网络交互所需的方法。

“ sendnewmsg”方法用于将消息发送到主题中,其他同行可以在其中订阅和读取消息。

index.js调用并执行该方法。

您能帮我解决问题吗?

提前致谢!

ipfs.js:


const IPFS = require('ipfs');
const BufferPackage = require('buffer');

const Buffer = BufferPackage.Buffer;

class IPFSEngine {

    constructor(){
        let node = IPFS.create({
            EXPERIMENTAL: { pubsub: true },
            repo: (() => `repo-${Math.random()}`)(),
            config: {
                Addresses: {
                    Swarm: [
                        '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
                    ]
                }
            }
        });

        this.node = node;
    }

    ....
    ....
    ....

    sendNewMsg(topic, newMsg) {
        let {node} = this;
        console.log('Message sent: ', newMsg)
        const msg = Buffer.from(newMsg)
        node.pubsub.publish(topic, msg, (err) => {
            if (err) {
                return console.error(`Failed to publish to ${topic}`, err)
            }
            // msg was broadcasted
            console.log(`Published to ${topic}`)
        })
    }
}

// Export IPFSEngine
module.exports = {IPFSEngine};

index.js:


const {IPFSEngine} = require('./ipfs');

const ipfs = new IPFSEngine();

ipfs.sendNewMsg(`topic2`,`Messages 2 ..!`)

I'm trying to send a message through an IPFS network but I keep getting the error TypeError: Cannot read properties of undefined (reading 'publish').

The error occurs at line node.pubsub.publish(topic, msg, (err) => {}).

ipfs.js is the app's main engine and contains the methods needed to interact with the IPFS network.

The 'sendNewMsg' method is used to send messages to a topic where other peers can subscribe and read the messages.

index.js calls and executes the method.

Could you please help me spot and fix the problem?

Thanks in advance!

ipfs.js:


const IPFS = require('ipfs');
const BufferPackage = require('buffer');

const Buffer = BufferPackage.Buffer;

class IPFSEngine {

    constructor(){
        let node = IPFS.create({
            EXPERIMENTAL: { pubsub: true },
            repo: (() => `repo-${Math.random()}`)(),
            config: {
                Addresses: {
                    Swarm: [
                        '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
                    ]
                }
            }
        });

        this.node = node;
    }

    ....
    ....
    ....

    sendNewMsg(topic, newMsg) {
        let {node} = this;
        console.log('Message sent: ', newMsg)
        const msg = Buffer.from(newMsg)
        node.pubsub.publish(topic, msg, (err) => {
            if (err) {
                return console.error(`Failed to publish to ${topic}`, err)
            }
            // msg was broadcasted
            console.log(`Published to ${topic}`)
        })
    }
}

// Export IPFSEngine
module.exports = {IPFSEngine};

index.js:


const {IPFSEngine} = require('./ipfs');

const ipfs = new IPFSEngine();

ipfs.sendNewMsg(`topic2`,`Messages 2 ..!`)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2025-01-29 13:10:11

该错误指出,当您尝试访问对象上的Publish属性时,node.pubsub是不确定的。

快速阅读,看来ipfs.create是一种异步API,结果您不在等待。它可以解释为什么您会在节点上获得未定义的pubsub属性。

使用异步函数,您可以编写类似的内容:

async function sendNewMsg(topic, newMsg) {
  let { node } = this;
  console.log('Message sent: ', newMsg);
  const msg = Buffer.from(newMsg);
  (await node).pubsub.publish(topic, msg, (err) => {
    if (err) {
      return console.error(`Failed to publish to ${topic}`, err);
    }
    // msg was broadcasted
    console.log(`Published to ${topic}`);
  });
}

或不带异步/等待语法:

function sendNewMsg2(topic, newMsg) {
  let { node } = this;
  console.log('Message sent: ', newMsg);
  const msg = Buffer.from(newMsg);
  node.then((readyNode) => {
    readyNode.pubsub.publish(topic, msg, (err) => {
      if (err) {
        return console.error(`Failed to publish to ${topic}`, err);
      }
      // msg was broadcasted
      console.log(`Published to ${topic}`);
    });
  })
}

The error states that node.pubsub is undefined while you are trying to access the publish property on the object.

Quickly reading through an example from the IPFS documentation, it appears that IPFS.create is an asynchronous API, which result you are not awaiting. It could explain why you get an undefined pubsub property on your node.

Using an async function, you could write something like:

async function sendNewMsg(topic, newMsg) {
  let { node } = this;
  console.log('Message sent: ', newMsg);
  const msg = Buffer.from(newMsg);
  (await node).pubsub.publish(topic, msg, (err) => {
    if (err) {
      return console.error(`Failed to publish to ${topic}`, err);
    }
    // msg was broadcasted
    console.log(`Published to ${topic}`);
  });
}

Or without the async/await syntax:

function sendNewMsg2(topic, newMsg) {
  let { node } = this;
  console.log('Message sent: ', newMsg);
  const msg = Buffer.from(newMsg);
  node.then((readyNode) => {
    readyNode.pubsub.publish(topic, msg, (err) => {
      if (err) {
        return console.error(`Failed to publish to ${topic}`, err);
      }
      // msg was broadcasted
      console.log(`Published to ${topic}`);
    });
  })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文