Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  61] [ 6]  / answers: 1 / hits: 25019  / 8 Years ago, thu, may 26, 2016, 12:00:00

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.


More From » typescript

 Answers
34

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


[#62013] Wednesday, May 25, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;