Romain Maussang Romain Maussang -4 years ago 84
Node.js Question

nodejs : How can I fill a global Array containing all objects of a kind?

Here is my problem :

I have a class Bot. I would like to fill it each time I create an object of this class. An then show it on a jade file. So far I have :

//bot.js
var listeBots = [];
exports.listeBots = listeBots;

// Constructeur
function Bot (nom, probavoyager, probatweet, probaretweet, nbhashtag, probalien, probalike, probamention, probaselfmention, visibilite,probaphoto) {
this.nom = nom;
this.probavoyager = probavoyager;
this.probatweet = probatweet;
this.probaretweet =probaretweet;
this.nbhashtag = nbhashtag;
this.probalien = probalien;
this.probalike = probalike;
this.probamention = probamention;
this.probaselfmention = probaselfmention;
this.visibilite = visibilite;
this.probaphoto = probaphoto;
}

exports.BotSuiveur = function(name){
Bot.call(this, name, 0.2, 0.2, 0.8, 1, 0.4, 0.8, 0.8, 0.2, 0.05, 0.1 );
listeBots.push(this);
//console.log(this);
};

exports.BotLeader = function(name){
Bot.call(this, name, 0.2, 0.8, 0.4, 2, 0.6, 0.4, 0.4, 0.8, 0.8, 0.2 );
listeBots.push(this);
//console.log(this);
};

function BotVoyageur() {
Bot.call(this, 0.8, 0.6, 0.4, 5, 0.4, 0.4, 0.4, 0.8, 0.15, 0.8 );
//console.log(this);
}

// jade file
each kw in listeBots
li= kw


My jade file just shows me Object[Object] and so on
How can I get the objects value visible in jade ?

Thanks in advance !
Romain

Answer Source

It is better to use constructors with new keyword and try to not to use this keyword in function as a variable.

//bot.js
var listeBots = [];
exports.listeBots = listeBots;

// Constructeur
function Bot (nom, probavoyager, probatweet, probaretweet, nbhashtag, probalien, probalike, probamention, probaselfmention, visibilite,probaphoto) {
    this.nom = nom;
    this.probavoyager = probavoyager;
    this.probatweet = probatweet;
    this.probaretweet =probaretweet;
    this.nbhashtag = nbhashtag;
    this.probalien = probalien;
    this.probalike = probalike;
    this.probamention = probamention;
    this.probaselfmention = probaselfmention;
    this.visibilite = visibilite;
    this.probaphoto = probaphoto;
}

exports.BotSuiveur = function(name){
    /* creates bot instance */
    var bot = new Bot(name, 0.2, 0.2, 0.8, 1, 0.4, 0.8, 0.8, 0.2, 0.05, 0.1 );
    listeBots.push(bot);
    //console.log(bot);
};

exports.BotLeader = function(name){
    /* creates bot instance */
    var bot = new Bot(name, 0.2, 0.8, 0.4, 2, 0.6, 0.4, 0.4, 0.8, 0.8, 0.2 );
    listeBots.push(bot);
    //console.log(bot);
};

function BotVoyageur() {
    /* creates bot instance */
    var bot = new Bot('name', 0.8, 0.6, 0.4, 5, 0.4, 0.4, 0.4, 0.8, 0.15, 0.8 );
    //console.log(bot);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download