javascript - getting error "property does not exist" -
i modified question more specific. don't care desired behavior , need correct syntax error
i studying this tutorial face error in code.
severity: 'error'
message: 'property 'offset' not exist on type 'pagerserviceprovider'.'
actually have same error 3 variables.
that.pagesize,that.offset,that.size
public async getpager(tablename:string,pagesize: number = 10) { let pagesize = pagesize; let offset = 0; let limit = pagesize; let size = await this.gettotal(tablename); let = this; return { initialpage:function(){ return new promise((resolve,reject)=>{ var d = []; that.executesql(tablename,limit,offset).then((data)=>{ console.log(json.stringify(data)); for(var = 0 ; < data.rows.length ; i++) { d.push(data.rows.item(i)); } resolve(d); },(e)=>{ reject(e); }); }); }, nextpage:function(){ if(that.offset <= that.size - that.pagesize ) { that.offset += that.pagesize; } return new promise((resolve,reject)=>{ var d = []; that.executesql(tablename,limit,offset).then((data)=>{ for(var = 0 ; < data.rows.length ; i++) { d.push(data.rows.item(i)); } resolve(d); },(e)=>{ reject(e); }); }); } };}
when use keyword function
declare function, function's not refer upper this
. using this
in function body, refers function itself.
the problem facing related fact function declared inside class have this
defined, need way reference upper this
while being inside nested function.
class test { hello () { console.log('hello') } method () { this.hello() // work because `this` refers class function sayhello () { return this.hello() // won't work because `this` refers function sayhello } return sayhello() } }
to bypass limitation, can save upper this
in variable while code in upper scope. variable called that
or self
.
class test { hello () { console.log('hello') } method () { var = // refering class this.hello() // work because `this` refers class function sayhello () { return that.hello() // still refering class succeed } return sayhello() } }
edit:
another trick avoid using that
use es6 arrow function. inside arrow function, this
alway refers upper scope.
class test { hello () { console.log('hello') } method () { this.hello() // work because `this` refers class // `this` refers upper scope default works const sayhello = () => this.hello() return sayhello() } }
edit 2:
your code should be:
public async getpager(tablename: string, pagesize: number = 10) { let pagesize = pagesize; let offset = 0; let limit = pagesize; let size = await this.gettotal(tablename); let = this; return { initialpage: function () { return new promise((resolve,reject)=>{ var d = []; that.executesql(tablename, limit, offset).then(data => { console.log(json.stringify(data)); for(var = 0 ; < data.rows.length ; i++) { d.push(data.rows.item(i)); } resolve(d); }, e => { reject(e); }); }); }, nextpage: function () { if(offset <= size - pagesize ) { offset += pagesize; // no need use `that` because used `let` } return new promise((resolve, reject) => { var d = []; that.executesql(tablename, limit, offset).then(data => { for(var = 0 ; < data.rows.length ; i++) { d.push(data.rows.item(i)); } resolve(d); }, e => { reject(e); }); }); } }; }
Comments
Post a Comment