I'm trying to create a type-ahead in LWC wherein I'm using imperative approach(this is what I want to use - not @wire) to make get matching record names from back-end. However, when I type anything in the search box I get the below error:
This page has an error. You might just need to refresh it. First, would you give us some details? (We're reporting this as error ID: -491200357)
Below is the code:
HTML:
<template>
<lightning-card title="Type-Ahead Imperative">
<lightning-input type="text" onchange={handleChange} value={sSearchKey}></lightning-input>
<template if:true={bShowAcctNames}>
<template for:each={lstAcctNames} for:item="sAcctName">
<p key={sAcctName}>{sAcctName}</p>
</template>
</template>
</lightning-card>
</template>
JS
import { LightningElement } from 'lwc';
import ACCT_NAMES from '@salesforce/apex/AccountData.getAccountNames'
export default class ImperativeTypeAhead extends LightningElement {
sSearchKey = '';
lstAcctNames = [];
bShowAcctNames = false;
handleChange(event){
this.sSearchKey = event.target.value;
ACCT_NAMES({sSearchKey : '$sSearchKey'})
.then(result =>{
result.forEach(sName => {
this.lstAcctNames.push(sName);
});
this.bShowAcctNames = true;
})
.catch(error=>{
// eslint-disable-next-line no-console
console.log('Error Occured : ',error);
})
}
}
The back-end call isn't made at all - I verified from debug logs. What's the issue here?
'$sSearchKey'
and will be sent to the server as such. Plus, no need to have a reactive var inside an event handler. Plus, you'd need to refer to it asthis.sSearchKey
event.detail.value
I think.'$sSearchKey'
is actually an accepted syntax, at least with an@api
variable, but I suspect it works just as well otherwise. I do agree there is no need to have a reactive property there, though