gpt4 book ai didi

javascript - TypeError : . currentPizzaPlace.getPoint 不是函数

转载 作者:行者123 更新时间:2023-12-01 02:21:38 31 4
gpt4 key购买 nike

类型错误:this.pizzaPlaceEditService.currentPizzaPlace.getPoint 不是函数

我找到了TypeError: is not a function typescript class但我认为这不适用于这种情况。又发现了几个似乎不适用的。这是我编辑过的类(class):

export class PizzaPlace {
deliveryArea = [];

getPoint(seq: number): Point {
let result: Point = null;

this.deliveryArea.forEach(function(point:Point) {
if(seq == point.sequence) {
result = point;
}
});

return result;
}

}

这是我的工作单元测试:

  it('should retrieve a point by sequence', () => {
var point1 = new Point();
point1.sequence = 0;
point1.latitude = 5.12;
point1.longitude = 5.12;

var point2 = new Point();
point2.sequence = 1;
point2.latitude = 6.13;
point2.longitude = 6.13;

var point3 = new Point();
point3.sequence = 2;
point3.latitude = 5.25;
point3.longitude = 5.25;

pizzaPlace.deliveryArea = [point1, point2, point3];

expect(pizzaPlace.getPoint(0)).toBe(point1);
expect(pizzaPlace.getPoint(1)).toBe(point2);
expect(pizzaPlace.getPoint(2)).toBe(point3);
expect(pizzaPlace.getPoint(3)).toBe(null);
});

这是生成错误的代码:

  onDownClick(): void {
if(this.selectedPoint) {
let currSeq = this.selectedPoint.sequence;
let nextSeq = currSeq + 1;
console.log("Current pizza place:" + JSON.stringify(this.pizzaPlaceEditService.currentPizzaPlace));
console.log("nextSeq:" + nextSeq);
let next = this.pizzaPlaceEditService.currentPizzaPlace.getPoint(nextSeq);
if(next) {
this.pizzaPlaceEditService.currentPizzaPlace.swapPoints(this.selectedPoint, next);
}
}
}

这是错误:

Current pizza place:{"contactName":"Zaphod Beeblebrox","customerId":"TPGup2lt","deliveryArea":[{"errorMsg":"","sequence":0,"latitude":34.552,"longitude":-84.556},{"errorMsg":"","sequence":1,"latitude":34.711,"longitude":-84.665}],"deliveryZips":[],"paypalAccount":"HB17","settings":null,"shopName":"Donato's Hartland","shopStreet":"1531 Kenesaw Dr","shopCity":"Lexington","shopState":"KY","shopZip":"40515","shopPhone":"(859)555-2637","billStreet":"1531 Kenesaw Dr","billCity":"Lexington","billState":"KY","billZip":"40515","billPhone":"(859)555-2637"}
pizza-place-delivery.component.ts:63:6
nextSeq:1
pizza-place-delivery.component.ts:64:6
TypeError: this.pizzaPlaceEditService.currentPizzaPlace.getPoint is not a function

我已经没有什么可以尝试的了。任何建议表示赞赏!

最佳答案

感谢 Artem 的出色输入,我能够弄清楚问题是由于我从 JSON 创建对象而不是我想要的对象而引起的,因此它是不同的类型。显然,Typescript 中的类仅在编译时存在,并在运行时被丢弃。

因此,基于:How do I initialize a TypeScript object with a JSON object所选答案的选项4,我创建了一个serialized.ts。

export interface Serializable<T> {
deserialize(input: Object): T;
}

然后用实现修改我的类:

export class PizzaPlace implements Serializable<PizzaPlace> {

然后添加:

  deserialize(input): PizzaPlace {
this.paypalAccount = input.paypalAccount;
this.customerId = input.customerId;
...
this.settings = input.settings;

return this;
}

然后,由于我的网络服务返回了这些数组,我更改了我的服务调用:

  .subscribe(places => this.deserializePlaces(places));

并添加了一个新功能:

  deserializePlaces(places: Object[]){
this.pizzaPlaces = [];

places.forEach(obj=> {
let place = new PizzaPlace().deserialize(obj);
this.pizzaPlaces.push(place);
});
}

这似乎工作得很好。感谢您的投入。

关于javascript - TypeError : . currentPizzaPlace.getPoint 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49163375/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com