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
jrvjrv 

How to update result from wired function?

I'm using a wired function to pull rows from a custom object, but I'm not sure how to edit the response.  This is what I have now:
 
@wire(searchSmartTasks, { smartTaskName: '$smartTaskName',  calendarValue: '$calendarValue',})
    searchSmartTasks({ error, data }) {
        if (data) {
            for(let i = 0; i < this.data.length; i++) {
                this.data[i].classStatus = 'green';
            }

        } else if (error) {
            this.error = error;
            console.log(error);
        }
    }
My SmarTask object doesn't have a field named classStatus, so I'm trying to append it to the response, so I can access it in my view like this:
 
<ul>
		<template for:each={smartTasks} for:item="smartTask">
			<li key={smartTask.Id}>
				{smartTask.classStatus}
			</li>
		</template>
	</ul>
The above doesn't work.  Any ideas?

From looking over a few sources online it seems the returned data is not editable and the suggestion was to clone the data by doing something like this:
 
// ADDED THIS LINE
@track smartTasks = [];

@wire(searchSmartTasks, { smartTaskName: '$smartTaskName',  calendarValue: '$calendarValue',})
    searchSmartTasks({ error, data }) {
        if (data) {
           
           // ADDED THIS LINE
           this.smartTasks = {...data};
           
           for(let i = 0; i < this.smartTasks.length; i++) {
                this.smartTasks[i].classStatus = 'green';
            }

        } else if (error) {
            this.error = error;
            console.log(error);
        }
    }



This unfortunately gives me the following error when the searchSmartTasks function is called:

[Assert Violation: Invalid template iteration for value `[object Object]` in [object:vm undefined (46)]. It must be an array-like object and not `null` nor `undefined`.]
MagulanDuraipandianMagulanDuraipandian
Check the return type of the result from apex. I think it is map. If it is not map, then list of records will be in data variable. Also make sure the returned value is not null.

--
Magulan Duraipandian
www.infallibletechie.com
jrvjrv
Thanks for taking a look!

On the Apex side I'm actually just returning a List like this:
 
List<smart_task__c> searchResults = [SELECT Id, Name FROM smart_task__c WHERE Name LIKE :smartTaskName LIMIT 5];

Also tried setting this.smartTasks to data.data, but no luck.  Any other ideas?
 
jrvjrv
Ended up changing my approach. I now pass in values to another child LWC to get the desired results.