Typescript错误类型'ArrayBuffer'无法分配给类型'Item []'

时间:2018-08-25 08:28:45

标签: typescript ionic3

在我的离子项目中,我想从PostgreSQL数据库的表中获取行,然后将行分配给item []:currentItems。

我检查来自postgreSql的返回行,其格式为:

foreach($kycs as $kyc)  && oreach($kycs as $key => $kyc)

但是,当我将其分配给item []:currentItems时,出现以下错误: Typescript错误类型'ArrayBuffer'无法分配给类型'Item []'。

下面是我的inoic代码的一部分:

item.ts:

[
 anonymous 
 {
   name: jack,
   email: jack@gmail.com
 }
 anonymous 
 {
   name: mark,
   email: mark@gmail.com
 }
]

list-master.ts:

export class Item {

  constructor(fields: any) {
    // Quick and dirty extend/assign fields to this model
    for (const f in fields) {
      // @ts-ignore
      this[f] = fields[f];
    }
  }

}

items.ts

export class ListMasterPage {
  currentItems: Item[];

  params: { uid: string } = {
    uid: "test"
  };

  constructor(public navCtrl: NavController,
    public items: Items,
    public modalCtrl: ModalController,
    public globalvar: GlobalvarProvider,
    public toastCtrl: ToastController) {

    this.params.uid = this.globalvar.userIdentifier
    this.items.query(this.params).subscribe((resp) => {
    /* here assign the received rows from database to the currentItems */
      this.currentItems = resp;
    }, (err) => {
    });;
  }

谢谢

1 个答案:

答案 0 :(得分:1)

看来您解析JSON时可能拥有有效的普通对象。要解决TypeScript为什么认为类型为get的问题,我需要查看Items.prototype.query方法中调用的Item方法的声明。如果您想要this.currentItems = (<any[]>resp).map((i) => new Item(i)); 对象而不是普通对象,则必须自己实例化它们,例如:

{{1}}

或使用序列化库。

相关问题