在Typescript中实例化类时,为什么我的代码卡住了?

时间:2018-10-15 14:01:19

标签: javascript typescript object

我正在解析一些数据的字符串,然后根据该数据创建对象。我能够很好地解析数据,但是由于某种原因,当我尝试创建新对象时,我的代码停顿了吗?我怀疑我没有正确执行此操作,这是我的代码:

这是我的阅读日期班级:

class ReadingDate {
year: number;
month: number;
day: number;
hour: number;
minute: number;
second: number;

public constructor(y: number, m: number,
                 d: number, h: number,
                 min: number, s: number) {
  this.year = y;
  this.month = m;
  this.day = d;
  this.hour = h;
  this.minute = min;
  this.second = s;
}

public toString() : string{
    return (this.year+"-"+this.month+"-"+this.day+" "+this.hour+":"+this.minute+":"+this.second);
  }
 }

这是一段代码,我尝试在另一个类中创建一个新的ReadingDate:

static parseDate(date: string){
  console.log("parsing date"); // this prints fine
  let array = date.split(" ", 2);
  let dateArray = array[0].split("-", 3);
  let timeArray = array[1].split(":", 3);
  console.log("numbers \n1: " + parseInt(dateArray[0]) +" type: " + typeof(parseInt(dateArray[0]))
    +"\n2: " + parseInt(dateArray[1]) + " type: " + typeof(parseInt(dateArray[1]))
    +"\n3: " + parseInt(dateArray[2]) + " type: " + typeof(parseInt(dateArray[2]))
    +"\n4: " + parseInt(timeArray[0]) + " type: " + typeof(parseInt(timeArray[0]))
    +"\n5: " + parseInt(timeArray[1]) + " type: " + typeof(parseInt(timeArray[1]))
    +"\n6: " + parseInt(timeArray[2]) + " type: " + typeof(parseInt(dateArray[2]))
 );
  console.log("parsed date"); // this prints fine
  var parsedDate = new ReadingDate(
    parseInt(dateArray[0]),
    parseInt(dateArray[1]),
    parseInt(dateArray[2]),
    parseInt(timeArray[0]),
    parseInt(timeArray[1]),
    parseInt(timeArray[2])
  );
  console.log("reached here"); //this never prints
  console.log("newlyParsedDate: "+ parsedDate.toString()); //this never prints
  return parsedDate; //this never happens
}

这是我调用parseDate()的示例:

static parseReading(reading: string) {
    console.log("inparser: "+reading);
    let array = reading.split(",", 4);
    let date = ParserProvider.parseDate(array[0]);
    console.log("finished parsing date"); //this does not print and nothing past it happens
    let location = array[1];
    let val = array[2]==1?true:false;
    let battery = array[3];
    console.log("inparser: "+location,val,battery); //does not print

}

示例日期为“ 2018-10-16 10:32:02”,控制台输出如下所示:

parser.ts:17 inparser: 2018-10-16 10:41:22,living,1,16
parser.ts:31 parsing date
parser.ts:33 2018-10-16
parser.ts:34 10:32:02
parser.ts:37 parsed date
parser.ts:38 numbers 
             1: 2018 type: number
             2: 10 type: number
             3: 16 type: number
             4: 10 type: number
             5: 32 type: number
             6: 2 type: number

它没有给我任何错误,但是在我尝试创建ReadingDate对象之后,我的程序似乎停止执行任何操作。

在此先感谢您的帮助!

编辑:使用toString方法更新

编辑2:更新了parseDate被调用的示例,一些额外的console.logs和被调用时的控制台输出

编辑3:好的,所以当我在试图使对象变成这样的地方放上try / catch时:

try{var parsedDate = new ReadingDate(
      parseInt(dateArray[0]),
      parseInt(dateArray[1]),
      parseInt(dateArray[2]),
      parseInt(timeArray[0]),
      parseInt(timeArray[1]),
      parseInt(timeArray[2])
      );}
catch(error){console.log(error);}

我收到此错误:

ReferenceError: ReadingDate is not defined
at Function.webpackJsonp.197.ParserProvider.parseDate (VM14753 main.js:300)
at Function.webpackJsonp.197.ParserProvider.parseReading (VM14753 main.js:276)
at HomePage.webpackJsonp.195.HomePage.handleReading (VM14753 main.js:146)
at VM14753 main.js:141
at VM14752 vendor.js:75337
at Array.forEach (<anonymous>)
at Events.publish (VM14752 vendor.js:75336)
at ClientImpl.MqttProvider.onMessageArrived (VM14753 main.js:228)
at ClientImpl.LibraryFactory.ClientImpl._receiveMessage (VM14750 paho-mqtt.js:1502)
at ClientImpl.LibraryFactory.ClientImpl._receivePublish (VM14750 paho-mqtt.js:1477)

编辑4:已修复!请参阅下面的答案,并参考此StackOverflow问题here

1 个答案:

答案 0 :(得分:1)

非常感谢您的所有帮助,特别是@osama,它建议您尝试使用/挂接查找错误。原来我需要做的就是导出ReadingDate类,然后将其导入我的解析器文件中。似乎是与此帖子here相同/相似的问题。