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
BB 

how to set a map attribute in aura

I want to set a map attribute in aura  but it keeps not setting as supposed 

aura attribute i tried without default as well
    <aura:attribute name="mapDistances" type="Map" default="{}"/>

js controller 

        var mapAccountsToDistance  = new Map();
        // i checked this and is populated as it should key ->  value 
        mapAccountsToDistance.set(account.Id,haversineDistance); 
          
       component.set("v.mapDistances",mapAccountsToDistance);
        
        var mpatest = component.get("v.mapDistances"); // and this gives me error and not getting any values 

Any ideas ? 




 
Best Answer chosen by B
Meghna Vijay 7Meghna Vijay 7
Hi, 
I tried it in my org and it's working . Try it like this:-
var mpatest = component.get("v.Map");
       mpatest["001D000002DklUgIAJ"] = "94.48"; 
       component.set("v.Map",mpatest);       
        console.log(mpatest);
I am getting [object object] in component when i added map like <p>{!v.Map}</p>

Hope it helps.
Thanks
        
       

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi,
In console log are you getting values in mapAccountsToDistance? And can you tell me more about how are you getting account.Id and haversineDistance?
Thanks
BB
Hi,

mapAccountsToDistance is populated correctly ex 

key: "001D000002DklUgIAJ"
value: "94.48"

The problem is that the component.set is not working  

Error when i try to  component.get("v.mapDistances"); 
[Exception: TypeError: Method get Map.prototype.size called on incompatible receiver #<Map> at Map.get size [as size] (<anonymous>) at Map.invokeGetter (<anonymous>:2:14)
Meghna Vijay 7Meghna Vijay 7
Hi,
Can you try  mapAccountsToDistance[account.Id] = haversineDistance instead of mapAccountsToDistance.set(account.Id,haversineDistance); ?
Thanks
 
BB
I already tried that , sorry for forget to mention :( and it didn't work 
Meghna Vijay 7Meghna Vijay 7
Hi, 
I tried it in my org and it's working . Try it like this:-
var mpatest = component.get("v.Map");
       mpatest["001D000002DklUgIAJ"] = "94.48"; 
       component.set("v.Map",mpatest);       
        console.log(mpatest);
I am getting [object object] in component when i added map like <p>{!v.Map}</p>

Hope it helps.
Thanks
        
       
This was selected as the best answer