node.js ipfs发送消息typeerror:无法读取未定义的属性(Reading' Prublish Qupprance')
我正在尝试通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误指出,当您尝试访问对象上的
Publish
属性时,node.pubsub
是不确定的。快速阅读,看来
ipfs.create
是一种异步API,结果您不在等待。它可以解释为什么您会在节点上获得未定义的pubsub
属性。使用异步函数,您可以编写类似的内容:
或不带异步/等待语法:
The error states that
node.pubsub
is undefined while you are trying to access thepublish
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 undefinedpubsub
property on your node.Using an async function, you could write something like:
Or without the async/await syntax: