Map.get_and_update

I am trying to update a map before rendered as json, but it is not working this way.

defp login_reply({:ok, user}, conn) do
    with nil <- user.image_url do
        user = Map.get_and_update(user, :image_url, fn current_value ->
                        {current_value, "<svg>image</svg>"}
                       end)
        conn
        |> render("show.json", user: user)
    end 
end

Map.get_and_update returns a tuple not map. You should use Map.updateor Map.put

1 Like

Map.get_and_update does not refer to “get the current value for the update function and replace it with my changes”, but rather literally “get me the current value and at the same time update the map”. If you just want to updates something in the map go for Map.update or Map.update!

1 Like