Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push item to [string] in TypeScript

I want to add items to [string]. But the following code fails at param.push statement.

EDIT

declare var sqlitePlugin:any;
var query: string = `SELECT * FROM items `;

var param: [string];

if (options['limit']) {
  var limit = options['limit'];
  query = query + " LIMIT ? ";
  param.push(String(limit));
}

if (options['offset']) {
  var offset = options['offset'];
  query = query + " OFFSET ? ";
  param.push(String(offset));
}

sqlitePlugin.openDatabase({name: 'Items.db', key: 'Password', location: 'default'}, (db) =>  {
  db.transaction((tx)=> {
    tx.execQuery(query, param, (resultSet)=>{
    this.items = [];
      for(let i = 0; i < resultSet.rows.length; i++) {
        var item: Item = new Item();
        item.code = resultSet.rows.item(i).item_code;
        item.name = resultSet.rows.item(i).item_name;
        this.items.push(item);
      }
      callback(this.items);
    } );
  }
});

Sorry to ask this very basic question but I'm struggling for 2 days.. Please give me any hint or link.

Thanks in advance.

like image 638
tmrex7 Avatar asked May 26 '16 12:05

tmrex7


1 Answers

Try:

var param: string[] = [];

Check this snippet out, it shows the desired result. The issue is you're just not initialising the variable param, so .push doesn't exist for undefined.

Also you weren't declaring the array properly (see difference above). There are two ways to do so, taken from TypeScript's documentation:

TypeScript, like JavaScript, allows you to work with arrays of values. Array types can be written in one of two ways. In the first, you use the type of the elements followed by [] to denote an array of that element type:

let list: number[] = [1, 2, 3];

The second way uses a generic array type, Array:

let list: Array<number> = [1, 2, 3];

And here is the relevant documentation on TS site

like image 120
gdgr Avatar answered Sep 17 '22 11:09

gdgr