亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

nodejs推送不是一個功能

nodejs推送不是一個功能

料青山看我應如是 2022-01-13 16:57:16
我很難收集一個班級匹配示例和比賽匹配是匹配的集合我的班級比賽:const uuid = require("uuid");// Match class is a single game Match structureclass Match {    constructor(players) {      this.id = uuid.v4().toString();      this.players = players;    }    // Match rest methods...    // I.E: isMatchEnded, isMatchStarted ...  }  module.exports = Match;我的班級比賽class Matches {    constructor() {      this.matches = {};    }    addMatch(match) {      this.matches.push(match);    }    // Matches rest methods...   }  module.exports = Matches;我的主要:    const matches = new Matches();    const queue = new Queue();    queue.addPlayer(new Player(1,'spt',970));    queue.addPlayer(new Player(2,'test2',1000));    queue.addPlayer(new Player(3,'test3',1050));    queue.addPlayer(new Player(4,'test4',70));    const playerOne = queue.players.find((playerOne) => playerOne.mmr === 970);    const players = queue.searching(playerOne);    if(players){      const match = new Match(players);      matches.addMatch(match);    }console.log(matches);但我收到此錯誤:Matches.js:7      this.matches.push(match);                   ^TypeError: this.matches.push is not a function
查看完整描述

2 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

你的類的matches屬性Matches不是一個數組,它是一個對象。您需要更改它以初始化數組:


class Matches {

    constructor() {

      this.matches = [];

//-------------------^^

    }


    addMatch(match) {

      this.matches.push(match);

    }

    // Matches rest methods...   }


  module.exports = Matches;

您可以將其保留為一個對象,但您需要將一個鍵關聯到您使用該addMatch函數添加的每個匹配項:


class Matches {

    constructor() {

      this.matches = {};

    }


    addMatch(match) {

      this.matches[someUniqueKey] = match;

      //ex this.matches[match.id] = match

    }

    // Matches rest methods...   }


  module.exports = Matches;


查看完整回答
反對 回復 2022-01-13
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

在Node中,為了使用push,在你的類“匹配”中你必須聲明一個ARRAY [],而不是一個對象{},然后你可以使用push。


class Matches {

    constructor() {

      this.matches = []; //this is an ARRAY [] , not an Object {}

    }


    addMatch(match) {

      this.matches.push(match); //you need first declare the array

    }


希望能幫助到你!!


查看完整回答
反對 回復 2022-01-13
  • 2 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號