IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

NodeJS Discussion :

Pas de Array.indexOf() dans node.js?


Sujet :

NodeJS

  1. #1
    Inactif  
    Homme Profil pro
    Collégien
    Inscrit en
    Octobre 2012
    Messages
    78
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Collégien

    Informations forums :
    Inscription : Octobre 2012
    Messages : 78
    Points : 0
    Points
    0
    Par défaut Pas de Array.indexOf() dans node.js?
    Bonjour, j'ai ce message d'erreur :
    Code JavaScript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    /Users/solalrastier/tnl/obj/main.js:15
          while (__indexOf.call(this.games, game) >= 0) {
                           ^
    TypeError: Array.prototype.indexOf called on null or undefined
        at indexOf (native)
        at People.buy (/Users/solalrastier/tnl/obj/main.js:15:24)
        at Object.<anonymous> (/Users/solalrastier/tnl/obj/main.js:59:57)
        at Object.<anonymous> (/Users/solalrastier/tnl/obj/main.js:70:4)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Function.Module.runMain (module.js:497:10)
        at startup (node.js:119:16)
    Pouvez-vous m'aider?

  2. #2
    Expert éminent
    Avatar de sekaijin
    Homme Profil pro
    Urbaniste
    Inscrit en
    Juillet 2004
    Messages
    4 205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Urbaniste
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2004
    Messages : 4 205
    Points : 9 127
    Points
    9 127
    Par défaut
    le messages d'erreur est clair

    tu fais null.indexOf(...); ou undefined.indexOf(...);.

    vérifie l'existence de ton objet avant d'appeler la méthode.

    et pourquoi faire __indexOf.call(this.games, game) et non this.games.indexOf(game) ?

    A+JYT

  3. #3
    Inactif  
    Homme Profil pro
    Collégien
    Inscrit en
    Octobre 2012
    Messages
    78
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Collégien

    Informations forums :
    Inscription : Octobre 2012
    Messages : 78
    Points : 0
    Points
    0
    Par défaut
    Code JavaScript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    // Generated by CoffeeScript 1.7.1
    (function() {
      var People, genre, genres, peoples, sharers, _i, _len,
        __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
     
      People = (function() {
        function People(n) {
          this.n = n;
          this.money = (Math.random() * 5000) + 5000;
          this.interest = genres[Math.round(Math.random())];
        }
     
        People.prototype.buy = function() {
          var game, seller;
          while (__indexOf.call(this.games, game) >= 0) {
            game = ["Proprietary adventure", "Free adventure", "Proprietary sandbox", "Free sandbox", "Proprietary " + this.interest, "Free " + this.interest][Math.floor(Math.random() * 7)];
          }
          seller = sharers[Math.round(Math.random() * sharers[game].length)];
          peoples[seller.n].money += seller.price;
          this.money -= seller.price;
          if (game[0] === 'F' && (Math.round(Math.random() * 5)) === 5) {
            sharers[game].push([this.n, Math.round(Math.random() * 5 === 5) ? Math.round(Math.random() * 499 + 1) : 0]);
          }
          return this.games.push(game);
        };
     
        return People;
     
      })();
     
      sharers = [];
     
      genres = ['sandbox', 'adventure'];
     
      peoples = [
        {
          money: 500000,
          money: 500000
        }
      ];
     
      for (_i = 0, _len = genres.length; _i < _len; _i++) {
        genre = genres[_i];
        sharers["Proprietary " + genre] = [peoples[0]];
        sharers["Free " + genre] = [peoples[1]];
      }
     
      (function() {
        var i, _results;
        i = 0;
        _results = [];
        while (i < 100) {
          _results.push(peoples[i] = new People(i++));
        }
        return _results;
      })();
     
      while (true) {
        peoples[Math.floor(Math.random() * peoples.length)].buy();
        if (peoples[0].money === -500000) {
          console.log("Free");
          break;
        }
        if (peoples[0].money === -500000) {
          console.log("Proprietary");
          break;
        }
      }
     
    }).call(this);
    C'est un code auto-généré par le compilateur CoffeeScript à partir de :
    Code CoffeeScript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    class People
            constructor: (@n) ->
                    @money = (do Math.random * 5000) + 5000
                    @interest = genres[Math.round do Math.random]
            buy: ->
                    while game in @games #Until he finds a game he have not
                            game = ["Proprietary adventure", "Free adventure", "Proprietary sandbox", "Free sandbox", "Proprietary #{@interest}", "Free #{@interest}"][Math.floor do Math.random * 7] #Search a random game
     
                    seller = sharers[Math.round do Math.random * sharers[game].length]
                    peoples[seller.n].money += seller.price
                    @money -= seller.price
     
                    sharers[game].push [@n, if Math.round do Math.random * 5 is 5 then Math.round do Math.random * 499 + 1 else 0] if game[0] is 'F' and (Math.round do Math.random * 5) is 5 #If the game is free AND he wants share this game, he share this game
     
                    @games.push game
     
    sharers = []
    genres = ['sandbox', 'adventure']
     
    #Companies creation
    peoples = [money:500000, money:500000]
     
    #Selling creation
    for genre in genres
            sharers["Proprietary #{genre}"] = [peoples[0]]
            sharers["Free #{genre}"] = [peoples[1]]
     
    #Gamers creation
    do ->
            i = 0
            peoples[i] = new People i++ while i<100
     
    #Action!
    while true
            do peoples[Math.floor do Math.random * peoples.length].buy
            (console.log "Free";break) if peoples[0].money is -500000
            (console.log "Proprietary";break) if peoples[0].money is -500000

  4. #4
    Expert éminent
    Avatar de sekaijin
    Homme Profil pro
    Urbaniste
    Inscrit en
    Juillet 2004
    Messages
    4 205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Urbaniste
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2004
    Messages : 4 205
    Points : 9 127
    Points
    9 127
    Par défaut
    @games n'et pas initialisé dans ton constructeur
    donc il n'existe pas
    Tu ne peux donc pas faire un indexOf sur quelque chose qui n'existe pas.

    A+JYT

  5. #5
    Inactif  
    Homme Profil pro
    Collégien
    Inscrit en
    Octobre 2012
    Messages
    78
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Collégien

    Informations forums :
    Inscription : Octobre 2012
    Messages : 78
    Points : 0
    Points
    0
    Par défaut
    Merci. Bon, il y a un autre problème mais je le poserais sur un autre topic.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. indexOf dans un array
    Par SpaceFrog dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 21/09/2006, 11h46
  2. Réponses: 7
    Dernier message: 25/01/2006, 21h37
  3. Réponses: 1
    Dernier message: 05/10/2004, 15h51
  4. Réponses: 5
    Dernier message: 02/08/2004, 17h11
  5. [CR8.5] Ne peut pas insérer de carte dans un rapport
    Par liberio dans le forum SAP Crystal Reports
    Réponses: 5
    Dernier message: 14/06/2004, 22h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo