0

I'm new to lwc salesforce
I have an apex method called from js and then showed in html that take some time because it contain a soql, the method works when I select a combobox value

 <lightning-combobox
      name="template"
      label="Select Template"
      value={selectedTemplate}
      placeholder="Select Template"
      options={filteredTemplates}
      onchange={handleTemplateChange}
    ></lightning-combobox>

and showing the results in html like this these lightning-input need to be shown already so that's why I'm not using conditional template

    <lightning-input
      label="Objet message"
      value={template.Subject}
      onchange={handleInputChange}
      data-id="emailSubject"
    >
    </lightning-input>

    <lightning-textarea
      value={emailTextAreaValue}
      onchange={handleInputChange}
      data-id="emailTextAreaValue"
    >
    </lightning-textarea>

this is the js

  @track template;
  @track attachs = [];
.....

 handleTemplateChange(event) {

 this.selectedTemplateId = event.detail.value;
    getAll({ templateId: event.detail.value })
      .then((data) => {
        this.template = data.emailTemplate;
        this.attachs = data.attachments.map((item) => ({
          label: item.ContentDocument.Title,
          value: item.Id
        }));
        this.emailTextAreaValue = data.renderedEmailTemplateBody;

        // Dispatch a custom event to notify parent or other components
        const templateChangeEvent = new CustomEvent("templatechange", {
          detail: {
            templateId: this.selectedTemplateId,
            emailTemplate: this.template,
            attachments: this.attachs,
            renderedEmailTemplateBody: this.emailTextAreaValue
          }
        });
        this.dispatchEvent(templateChangeEvent);
      })
      .catch((error) => {
        console.error("Error fetching template content:", error);
      });
  }

but when I'm inspecting I'm getting

TypeError: Cannot read properties of undefined (reading 'Subject')

1 Answer 1

1

First of all, when you declared 'template' variable this way;

@track template; //undefined

You are declaring the variable to be undefined. If you are wondering why this is undefined, although declared, you can refer to the documentation from MDN. I have taken some important quote from the linked resource and quoted it below:

A variable that has not been assigned a value is of type undefined -MDN

This, however, is not the same as declaring it as an object, as now, you are assigning an empty object to this new variable.

@track template = {}; //empty object

As such, in your <lightning-input> when you trying to access the template.Subject, the tag is resolving in error, as you are trying to get the property of an undefined variable.

To fix this, initiate your 'template' variable as object as shown above.

Additionally, I would suggest you to NOT name your variable as template as this would clash with the way, how you would query HTML elements in JavaScript (particularly in LWC context), refer to example below. You can also refer to the documentation here.

this.template.querySelector("div");'
3
  • by changing @track template = {}; //empty object it worked thank you so much Commented Aug 30, 2024 at 16:12
  • @JsNinja If your problem has been resolved, you should accept this answer (there's a checkmark under the upvote/downvote area of each answer). That gives both the answerer and you reputation points, and signals to the community that you don't need additional help. Commented Aug 30, 2024 at 16:52
  • @DerekF done thank you Commented Aug 31, 2024 at 17:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.