错误:无法读取未定义的属性推送

时间:2021-01-18 17:22:01

标签: javascript node.js arrays constructor static

我正在尝试导入一个将在两个不同类中使用的静态数组,但是当我尝试通过数组参数推送参数时,它给了我一个错误。

头等舱:

const ChargingStation = require('./chargingStation')           
class Scooter {
  constructor(scooterID, color, speed, battery)
  {
    if (!scooterID) {
      throw new Error("Please insert Scooter ID")
    }

    this.scooterID = scooterID
    this.color = color
    this.speed = speed
    this.battery = battery
    ChargingStation.allScooter.push(scooterID) // <--- This is the line that throws the error
  } 
  module.exports = Scooter

第二类:

class ChargingStation {
  static allScooter = []
    constructor() {}
    module.exports = ChargingStation
}

1 个答案:

答案 0 :(得分:0)

我将根文件夹中的 ChargeStation 名称更改为 ChargingStation,重新启动我的 IDE 并开始工作。