2 回答

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;

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
}
希望能幫助到你!!
添加回答
舉報