8

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.

4
  • Look at Arrays in type script , may be this will help you. May 26, 2016 at 12:29
  • @tomo, it'd be better not to change the scope of the question. The original question you asked, I answered. You probably ought to ask a separate question for this particular issue you're facing now :)
    – gdgr
    May 26, 2016 at 15:03
  • okay, thanks @gdgr
    – raimtoon
    May 26, 2016 at 15:04
  • no problem @tomo, I requested an edit to the question but feel free to take it back to revision 2 :)
    – gdgr
    May 26, 2016 at 15:05

2 Answers 2

11

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

6
  • I tried var param : string[] but it doesn't work because param valuable is set to a function and syntax error occurs 'Argument of type string[] is not assignable to parameter of type [string]...'
    – raimtoon
    May 26, 2016 at 13:58
  • You're welcome. What about the updated code where you got the error you just mentioned?
    – gdgr
    May 26, 2016 at 14:14
  • I got syntax error at tx.execQuery(query, param, (resultSet)=>{.
    – raimtoon
    May 26, 2016 at 14:15
  • But I only see this line declaring param.. var param: [string];
    – gdgr
    May 26, 2016 at 14:20
  • sorry, its typo. currently it's var param: string[];
    – raimtoon
    May 26, 2016 at 14:25
0

You can use either of the below 2 syntaxes to define a string array in typescript:

Using general array syntax:

var param: string[] = [];

Using Generics syntax:

var param: Array<string> = [];
2
  • thanks. but as @gdgr 's answer, both sentences cause same syntax error I wrote in his comment.
    – raimtoon
    May 26, 2016 at 14:02
  • 1
    You need to initialise an array variable atleast to an emoty array in order to access any methods on it, irrespective of whether you use TypeScript or JavaScript May 26, 2016 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.