Hello, I hope you can help me to resolve my problem.
So I want to export an object from the file ‘hostconf.js’ and import it into my ‘routes.js’ file. But in this last file, my object is undefined. I think the problem is that the routes file is read before the hostconf file.
Is there a way to resolve this ?
My code from hostconf file :
getSystemInfo(function (manufacturer, model, serial){
hostSystem = [os.hostname(), manufacturer + ' ' + model, serial];
console.log('Hostconf File : ' + hostSystem);
module.exports = {hostSystem: hostSystem};
});
My code from routes file :
var express = require('express');
var router = express.Router();
var hostSystem = require('./hostconf').hostSystem;
console.log('Routes file : ' + hostSystem);
/* GET home page. */
router.get('/', function(req, res) {
res.render('kbagent-interface', { hostSystem: hostSystem});
});
My output :
Routes file : undefined
kbagent:server Listening on port 3000 +0ms
Hostconf File : PC, Acer Aspire, XTS6595DFF
Thank you very much
Note that assignment to module.exports must be done immediately. It cannot be done in any callbacks.
cf. https://nodejs.org/api/modules.html#modules_module_exports
I’m sorry, I don’t understand, what do you mean ?
The way you coded it is not supported by Node.js.
Oh ok… I’m a beginner and I tried to understand the operation of Node.js and the asynchronous mode.
How can I resolve my problem ? Can I export the function in antoher way ?
what function? you try to export a plain object.
don’t export inside a function.
Thank you.
Currently, even when exporting outside the function, the object is undefined (more exactly empty I think) in the router.
getSystemInfo(function (manufacturer, model, serial){
hostSystem = [os.hostname(), manufacturer + ' ' + model, serial];
console.log('Hostconf File : ' + hostSystem);
});
module.exports = {hostSystem: hostSystem};
Output :
Routes file :
kbagent:server Listening on port 3000 +0ms
Hostconf File : PC, Acer Aspire, XTS6595DFF
And I don’t understand how it works…
Can I export the object like that, or should I export the whole function ?
that’s because hostsystem
a) only exists in the callback and b) it may not exist at all when you do the assignment.
it depends. if you cannot get the hostsystem without a callback then you have to export the function that returns the hostsystem value.
Note: since I don’t know what getSystemInfo()
actually does, I can’t tell what it need to make it work.
Ok I understand.
Here is the code of the function getSystemInfo() :
var si = require('systeminformation');
var hostSystem =[];
function getSystemInfo(callback){
si.system(function (systemInfo) {
var manufacturer = systemInfo.manufacturer;
var model = systemInfo.model;
var serial = systemInfo.serial;
callback(manufacturer, model, serial);
//console.log(manufacturer + ' | ' + model + ' | ' + serial);
});
}
getSystemInfo(function (manufacturer, model, serial){
hostSystem = [os.hostname(), manufacturer + ' ' + model, serial];
console.log('Hostconf File : ' + hostSystem);
});
module.exports = {hostSystem: hostSystem};
This function retrieves machine informations from a nodejs module.
it’s not possible to export the hostsystem with this module. you better use the systeminformation module directly.
var express = require('express');
var router = express.Router();
var si = require('systeminformation');
router.get('/', function(req, res) {
si.system(function (systemInfo) {
res.render('kbagent-interface', systemInfo);
});
});
peet19
May 16, 2017, 1:31pm
11
Thank you for your help !
system
Closed
August 15, 2017, 8:32pm
12
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.