admin管理员组文章数量:1415655
I have declared a const 'offset', and when I try to fetch this and update in a function , the value is undefined. Hence I wont be able to add value to const. Below is sample code
import { LightningElement, wire, track , api } from 'lwc';
import getContractWithDetails from '@salesforce/apex/AccountContactController.getContractWithDetails';
const offset = 0;
export default class WrapperClassExampleLWC extends LightningElement {
@track accountsWithContacts;
@track contractWithDetails;
handleScroll(event)
{
console.log(' stats :' , this.offset);
}
}
handleScroll func is called when the user performs a scrolling action, and every time the scroll bar is reached to the bottom I want to update the offset value to higher value (say for example 30 every time) and fetch new records from apex and append to the table, but above console log gives me stats :undefined , why is this value undefined ? although I can check if the value is undefined and update with required value as below, I want to understand why is this value undefined even though I have set it to 0 , any links and pointers to SF LWC variable documentation would be helpful.
if(this.offset == undefined)
{
this.offset = 0;
}
this.offset = this.offset + 30;
I have declared a const 'offset', and when I try to fetch this and update in a function , the value is undefined. Hence I wont be able to add value to const. Below is sample code
import { LightningElement, wire, track , api } from 'lwc';
import getContractWithDetails from '@salesforce/apex/AccountContactController.getContractWithDetails';
const offset = 0;
export default class WrapperClassExampleLWC extends LightningElement {
@track accountsWithContacts;
@track contractWithDetails;
handleScroll(event)
{
console.log(' stats :' , this.offset);
}
}
handleScroll func is called when the user performs a scrolling action, and every time the scroll bar is reached to the bottom I want to update the offset value to higher value (say for example 30 every time) and fetch new records from apex and append to the table, but above console log gives me stats :undefined , why is this value undefined ? although I can check if the value is undefined and update with required value as below, I want to understand why is this value undefined even though I have set it to 0 , any links and pointers to SF LWC variable documentation would be helpful.
if(this.offset == undefined)
{
this.offset = 0;
}
this.offset = this.offset + 30;
Share
Improve this question
asked Nov 25, 2020 at 18:19
Suhas JainSuhas Jain
431 gold badge1 silver badge7 bronze badges
2 Answers
Reset to default 1const
can't be updated after declaration, experiment with https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/const
And since you have it outside of the class, like some global variable - it doesn't need this
. The script is telling you that this.offset
doesn't exist (undefined). You can console.log(offset);
I think what you need is to make it a class member and drop the const
you have declared the varible outside the class.
So you don't have to use this
to access the variable
So your function will look like below
handleScroll(event)
{
console.log(' stats :' , offset);
}
Note : As you are declaring variable as const
you can't change its value. if you want to change its value use let
本文标签: salesforcevariable values undefined in LWCJavaScriptStack Overflow
版权声明:本文标题:salesforce - variable values undefined in LWC - JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745176936a2646283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论