function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashutosh Tripathi 72Ashutosh Tripathi 72 

LWC load data from Controller at first load of component

Hi 

I want to set a variable from an apex class at the beginning of the component load.
I guess connctedCallBack should work for me. but as I am using a promise call there it's not working there, though if I write static code there, it works for me. What can I do to achieve this motive?

Code sample that I am using:
 
connectedCallback() {
        console.log('connectedCallback Called! ');
        //console.log(this.bookMarked);
        console.log('Item Id: ' + this.itemId);
        console.log('Checking if already bookmarked'); IsBookmarked({ sItemId: this.itemId })
            .then(result => {
                console.log(result);
                console.log(this.buttonText);
                if (result) {
                    this.buttonText = "Remove from bookmark";
                    this.updateButtonText('Remove from bookmark!');
                }
                else {
                    this.buttonText = "Bookmark Me!"
                    this.updateButtonText('Bookmark Me!');
                }
                console.log(this.buttonText);
            })
            .error(error => {
                console.log('IsBookmarked Failed:' + error.body.message);
            });

    }

 
B KarthickeyanB Karthickeyan
yes, you can instead of using connectedCallback() use constructor(). you can achieve.
constructor() {
        super();
	console.log('connectedCallback Called! ');
    //console.log(this.bookMarked);
    console.log('Item Id: ' + this.itemId);
    console.log('Checking if already bookmarked'); IsBookmarked({ sItemId: this.itemId })
        .then(result => {
            console.log(result);
            console.log(this.buttonText);
            if (result) {
                this.buttonText = "Remove from bookmark";
                this.updateButtonText('Remove from bookmark!');
            }
            else {
                this.buttonText = "Bookmark Me!"
                this.updateButtonText('Bookmark Me!');
            }
            console.log(this.buttonText);
        })
        .error(error => {
            console.log('IsBookmarked Failed:' + error.body.message);
        });
}
 Hope it helps you!
Ashutosh Tripathi 72Ashutosh Tripathi 72
@B Karthickeyan, I appreciate your reply. I have tried this method. But in this case, I won't able to access ItemId which is set from the community.