You need to sign in to do that
Don't have an account?

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 ?
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 ?
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
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
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)
Can you try mapAccountsToDistance[account.Id] = haversineDistance instead of mapAccountsToDistance.set(account.Id,haversineDistance); ?
Thanks
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