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. Commented 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 :) Commented May 26, 2016 at 15:03
  • okay, thanks @gdgr Commented 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 :) Commented 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

Sign up to request clarification or add additional context in comments.

6 Comments

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]...'
You're welcome. What about the updated code where you got the error you just mentioned?
I got syntax error at tx.execQuery(query, param, (resultSet)=>{.
But I only see this line declaring param.. var param: [string];
sorry, its typo. currently it's var param: string[];
|
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 Comments

thanks. but as @gdgr 's answer, both sentences cause same syntax error I wrote in his comment.
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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.