good Morning, I have a function that place a request to an elasticsearch REST API of a library management software, and I am trying to manipulate the response so I get the results(so far all OK), and the total results quantity(using a KErnel.tap/2 function for this). the response manipulation is as follows:
raw_response.body
|> Jason.decode!()
|> Map.fetch("hits")
|> elem(1)
|> tap(&get_total_results(&1, socket ))
|> Map.fetch("hits")
|> elem(1)
I use Kernel.tap/2 since the results is “hidden” in an upper structure of the response.
the get_total_results
function is as follows:
@impl true
def get_total_results(tr,socket) do
total_res =
tr["total"]
|> tap(&IO.inspect(&1))
socket =
socket
|> assign(results_qty: total_res)
end
as soon as I use this function I get this error:
** (ArgumentError) assign/3 expects a socket from Phoenix.LiveView/Phoenix.LiveComponent or an assigns map from Phoenix.Component as first argument, got: nil.
I have read the docs but have not got anyhing clear about this, I am using assigns update in otehr parts of this module and all is OK, I thought the problem was the socket id, but is the same that I use in mount/3 in the beggining of the code, the mount/3 function is this one.
use DedalosPhoenixWeb, :live_view
use Phoenix.HTML
alias DedalosPhoenixWeb.{OpacLive, NavBarLive}
def mount(params, _session, socket) do
{:ok,
assign(socket,
page_number: 1,
page_size: 10,
results_qty: 20,
query_params: params,
filtered_results: []
)}
end
Please anyone could clear this for me, I am still readeing the official docs to see If I get something clear, thanks in advance.