【问题标题】:How to fix 'Argument of type '{}' is not assignable to parameter of type 'any[]'. [ng] Property 'length' is missing in type '{}'.'如何修复 '{}' 类型的参数不可分配给 'any[]' 类型的参数。 [ng] 类型“{}”中缺少属性“长度”。
【发布时间】:2019-08-18 12:23:44
【问题描述】:

我即将在本教程的帮助下使用 Barcode Scanner 和 SQLite 构建一个 Ionic Inventory Management 应用程序:https://www.techiediaries.com/ionic-cordova-sqlite-barcode-scanner-product-inventory-manager/

当我添加这段代码时:

async createTables(){
    try {
        await this.database.executeSql(this.familyTable, {});
        await this.database.executeSql(this.locationTable,{});
        await this.database.executeSql(this.productTable,{});
        await this.database.executeSql(this.transactionTable,{});
    }catch(e){
        console.log("Error !");
    }
}

...到 data-service.service.ts 我得到了这个错误:

src/app/data-service.service.ts(54,64) 中的错误:错误 TS2345: “{}”类型的参数不能分配给“any[]”类型的参数。 [ng] 类型“{}”中缺少属性“长度”。 [ng] src/app/data-service.service.ts(55,65):错误 TS2345:类型参数 “{}”不可分配给“any[]”类型的参数。 [ng] 属性 “{}”类型中缺少“长度”。 [ng] src/app/data-service.service.ts(56,64):错误 TS2345:类型参数 “{}”不可分配给“any[]”类型的参数。 [ng] 属性 “{}”类型中缺少“长度”。 [ng] src/app/data-service.service.ts(57,68):错误 TS2345:类型参数 “{}”不可分配给“any[]”类型的参数。 [ng] 属性 “{}”类型中缺少“长度”。

这是整个 data-service.service.ts 代码:

import { Injectable } from '@angular/core';

import 'rxjs/add/operator/map';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';



@Injectable({
  providedIn: 'root'
})
export class DataServiceService {
  public database: SQLiteObject;

  productTable : string = `CREATE TABLE IF NOT EXISTS  products (
    id INTEGER PRIMARY KEY,
    sku TEXT,
    barcode TEXT,
    title TEXT NOT NULL,
    description TEXT,
    quantity REAL,
    unit VARCHAR,
    unitPrice REAL,
    minQuantity INTEGER,
    familly_id INTEGER,
    location_id INTEGER,
    FOREIGN KEY(familly_id) REFERENCES famillies(id),
    FOREIGN KEY(location_id) REFERENCES locations(id)
    );`;

familyTable : string = `CREATE TABLE IF NOT EXISTS famillies (
    id INTEGER PRIMARY KEY,
    reference VARCHAR(32) NOT NULL,
    name TEXT NOT NULL,
    unit VARCHAR);`;

locationTable : string = `CREATE TABLE IF NOT EXISTS locations (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL);`;
//Date , Quantity , Unit Cost , Reason (New Stock - Usable Return - Unusable Return ) ,UPC (Universal Product Code ) Comment    
transactionTable : string = `CREATE TABLE IF NOT EXISTS transactions (
        id INTEGER PRIMARY KEY,
        date TEXT,
        quantity REAL,
        unitCost REAL,
        reason VARCHAR,
        upc TEXT,
        comment TEXT,
        product_id INTEGER,
        FOREIGN KEY(product_id) REFERENCES products(id));`;

        async createTables(){
          try {
              await this.database.executeSql(this.familyTable, {});
              await this.database.executeSql(this.locationTable,{});
              await this.database.executeSql(this.productTable,{});
              await this.database.executeSql(this.transactionTable,{});
          }catch(e){
              console.log("Error !");
          }
      }

        constructor(public sqlite :SQLite) {
          console.log('Hello DataServiceProvider Provider')

              this.sqlite.create({name: "data.db", location: "default"}).then((db : SQLiteObject) => {
                      this.database = db;
                  }, (error) => {
                      console.log("ERROR: ", error);
              }); 
    }

}

有人知道如何解决吗?

【问题讨论】:

    标签: angularjs typescript sqlite ionic-framework error-handling


    【解决方案1】:

    executeSql 的签名是:

    executeSql(statement: string, params?: any[]): Promise<any>;
    

    如果您使用{} 作为第二个参数调用executeSql,我知道您不想传递任何参数。所以你应该这样调用方法:

    await this.database.executeSql(this.familyTable);
    await this.database.executeSql(this.locationTable);
    await this.database.executeSql(this.productTable);
    await this.database.executeSql(this.transactionTable);
    

    【讨论】:

    • 解决了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2021-12-05
    • 2019-09-06
    • 2019-11-02
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多