代码之家  ›  专栏  ›  技术社区  ›  Thom

类型错误:。当前PizzaPlace。getPoint不是函数

  •  0
  • Thom  · 技术社区  · 8 年前

    TypeError:此。pizzaPlaceEditService。当前PizzaPlace。getPoint不是函数

    我发现 TypeError: is not a function typescript 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
    

    我已经没有东西可以尝试了。感谢您的建议!

    1 回复  |  直到 8 年前
        1
  •  2
  •   Thom    8 年前

    多亏了Artem出色的输入,我才发现问题是由我从JSON创建对象而不是我想要的对象这一事实引起的,因此它是一种不同的类型。显然,Typescript中的类只是编译时的,在运行时会被丢弃。

    因此,基于: How do I initialize a TypeScript object with a JSON object 在所选答案的选项4中,我创建了一个可序列化的。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;
      }
    

    然后,由于我的web服务返回了其中的一个数组,我更改了我的服务调用:

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

    并添加了一个新功能:

      deserializePlaces(places: Object[]){
        this.pizzaPlaces = [];
    
        places.forEach(obj=> {
          let place = new PizzaPlace().deserialize(obj);
          this.pizzaPlaces.push(place);
        });
      }
    

    这似乎很管用。谢谢你的意见。

    推荐文章