0

I have two components one is for getting input(basically a contact name) on clicking that name it should show the contact details in the Subscriber component. I could see that for loop has some mistake as I noticed that console in for loop is not coming.

Apex:

public with sharing class searchContact {    
        @AuraEnabled(cacheable=true)
        public static List<Contact> getContact(String strSearchName){    
           String searchName = '%'+strSearchName+'%';    
            return [SELECT Id, Name, Phone,Email FROM Contact where Name LIKE  :searchName];            
        }    
}

Publisher:

<template>
    <lightning-card title="Search Contacts">
    <div class="slds-card slds-card_boundary slds-m-around_medium">
        <div class="slds-m-around_medium slds-grid slds-gutters">
            <lightning-input label="Name"  onchange={searchContact} ></lightning-input>
            </div>
            </div>
            <template if:true={contacts}>
                <template for:each={contacts} for:item="contact">
                    <li key={contact.Id}>                        
                            <a data-position={contact.Id} onclick={handleName}>{contact.Name}</a>                      
                    </li>
                </template>
            </template>
        </lightning-card>
</template>

import { LightningElement,track,api } from 'lwc';
import pubsub from 'c/pubsub' ; 
import getAllContact from '@salesforce/apex/searchContact.getContact'; 

const columns = [{
    label : 'Name',
    fieldName : 'Name',
    type : 'url',
    typeAttributes : {label : {fieldName : 'Name'}}},
    { label: 'Phone', fieldName: 'Phone', type: 'Phone' },
    { label: 'Email', fieldName: 'Email', type: 'Email' },
] ;
export default class SearchContactComp extends LightningElement {
    @track columns = columns;
    @track searchRecord;
    @api contacts;

        searchContact(event)
        {
            this.searchRecord = event.target.value;
            console.log(this.searchRecord);
            getAllContact({strSearchName : this.searchRecord}).then(response =>
                {
                    this.contacts = response;
                }).catch(error => {
                    console.log('There is an issue in loading the account');
                })
        }
        handleName(event){
            let position = event.target.dataset.position;
            console.log(position);
            for(let i=0; i<contacts.length;i++)
            {
                console.log('For loop');
                if(this.contacts[i].Id == position)
                {
            console.log("Record Detial");
            console.log(JSON.stringify(this.contacts[i]));
            pubsub.fire('showdetails',this.contacts[i]);
            console.log("Fired");
            }
        }
        }    }

Subscriber

<template>
    <lightning-card title="Subscriber">
        <div class="slds-p-around_large">
          Name : {contactRecord.Name}<br/>
          Email : {contactRecord.Email}<br/>
          Phone : {contactRecord.Phone}<br/>
        </div>
</lightning-card>
</template>

import { LightningElement,track } from 'lwc';
import pubsub from 'c/pubsub' ; 
import getAllContact from '@salesforce/apex/searchContact.getContact'; 
export default class SubComponent extends LightningElement {

   //COntact
    @track contactRecord={};
    
    connectedCallback(){
        pubsub.register('showdetails',this.handleEvent.bind(this));
        console.log('Subscriber');
    }

    handleEvent(payload){
        console.log('handle Event');
        console.log(JSON.stringify(payload));
        this.contactRecord=payload;
    }
}

1 Answer 1

0

The issue is with the line for(let i=0; i<contacts.length;i++).. here it should be for(let i=0; i<this.contacts.length;i++)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.