Tuesday, 16 May 2017

Undefined object after export - Node.js

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 :)



via SalutPelo

No comments:

Post a Comment