Learning how to develop modules I'm trying to learn how to export one from main.js. On my renderer I can send it correctly to main with:
renderer.js:
let _object = {
foo: foo1,
bar: bar1,
}
ipcRenderer.send('channel', _object)
in main.js I can get this correctly:
ipcMain.on('channel', (e, res) => {
console.log(JSON.stringify(res))
console.log(typeof res)
})
however when I export the result
from main.js and try to bring it into another file I get an undefined
:
main.js:
const foobar = require('./foobar')
ipcMain.on('channel', (e, res) => {
console.log(JSON.stringify(res))
console.log(typeof res)
module.exports.res = res
foobar.testing()
})
foobar.js:
const res = require('./main')
module.exports = {
testing: function(res) {
console.log(`Attempting console.log test: ${res}`)
console.log(res)
console.log(JSON.stringify(res))
}
}
terminal result:
Attempting console.log test: undefined
undefined
undefined
I've also tried to redefine the object in main.js:
ipcMain.on('channel', (e, res) => {
module.exports = {
foo: foo,
bar: bar,
}
console.log(`Testing object ${res.foo}`)
foobar.testing()
})
My research of reference:
What a I doing wrong in my export of the result
in main.js so I can use it in a different file?
My end goal is to learn how to be able to call res.foo
in foobar.js.
First of all, you have an argument res
in your testing
function, which shadows the import. Second, the imported res
object is all exports from main
, which includes the res
you need - so you should print res.res
instead of the whole object.
Foobar.js:
const res = require('./main')
module.exports = {
testing: function() {
console.log(`Attempting console.log test: ${res.res}`)
console.log(res.res)
console.log(JSON.stringify(res.res))
}
}
The last version (where you reassign module.exports
) would not work, because foobar
will still have the original exports, which was an empty object.