I am making a simple MAM publish and fetch script. The first script is mam_publish which publishes the data. The other script is mam_receive which fetched the already published data.
Now, the problem is that publishing script is working correctly but fetch script is not fetching the data. It is only executing the line
console.log('\n\nFETCHING DATA!!\n\n')
I tried debugging the code, but I am unable to figure out the problem. Can someone take a look?
Here is the script for mam_publish.js
const Mam = require('@iota/mam')
const { asciiToTrytes } = require('@iota/converter')
const IOTA = require('iota.lib.js');
const mode = 'restricted'
const secretKey = 'VERYSECRETKEY'
const provider = 'https://nodes.devnet.iota.org'
let numberofSteps;
let kilometersWalked;
let mamState = Mam.init(provider)
// Set channel mode
mamState = Mam.changeMode(mamState, mode, secretKey)
const publish = async packet => {
// Create MAM Payload - STRING OF TRYTES
const trytes = asciiToTrytes(packet)
const message = Mam.create(mamState, trytes)
// Save new mamState
mamState = message.state
// Attach the payload
await Mam.attach(message.payload, message.address, 3, 9)
console.log('Published', packet, '\n');
console.log ('Root', message.root);
//return message.root
}
const publishAll = async () => {
const root = await publish({
numberofSteps: Math.floor((Math.random()*89)+10),
kilometersWalked: (numberofSteps/1312.335),
message: 'Message from Alice',
timestamp: (new Date()).toLocaleString()
})
await publish({
numberofSteps: Math.floor((Math.random()*89)+10),
kilometersWalked: (numberofSteps/1312.335),
message: 'Message from Alice',
timestamp: (new Date()).toLocaleString()
})
return root
}
publishAll()
and the script for mam_receive
const Mam = require('./lib/mam.client.js');
const { trytesToAscii } = require('@iota/converter')
const IOTA = require('iota.lib.js');
const mode = 'restricted'
const secretKey = 'VERYSECRETKEY'
const provider = 'https://nodes.devnet.iota.org'
let root = 'SBOKXANECLJFR9QHUARPNFKJKRCEEQ9YACINNUPNOVTLZJCLUZN9ANTHZAATNCYLZGMVVVOBAUETPAHOZ';
// Initialise MAM State
let mamState = Mam.init(provider);
mamState = Mam.changeMode(mamState, mode, secretKey)
const logData = data => console.log('Fetched and parsed', JSON.parse(trytesToAscii(data)), '\n')
const execute = async root => {
await Mam.fetch(root, mode, secretKey, logData)
// Output synchronously once fetch is completed
const result = await Mam.fetch(root, mode, secretKey)
result.messages.forEach(message => console.log('Fetched and parsed', JSON.parse(trytesToAscii(message)), '\n'))
}
console.log('\n\nFETCHING DATA!!\n\n')
execute()
Any leads would be highly appreciated.
execute()
function takes aroot
parameter you are not passing... (you probably want to pass the globalroot
variable for it). Also probably you want to log yourresult
object before iterating over its (possibly empty) messages...root
variable inexecute()
function, but the result is same. By loggingresult
you mean useconsole.log()
?console.log('\n\nFETCHING DATA!!\n\n')
but not executing the execute function even though I passed a rootexecute(root)
. Any thing else that I might be overlooking?