the_dude

the_dude

Trying to load user data from the reddit api which is working. However, on mount it appears to be stuck in a rendering loop repeatedly making requests to the api. This causes it to error out and redirect to ‘/’. This is what I have below so far… running latest versions of phoenix and elixir 1.10.0. using Tesla for the api requests. i have also tried moving this logic to handle_params instead but does same thing. Any ideas why this is happening? I thought maybe it was making the call twice possibly since it connects to socket twice on initial mount but doesn’t seem to be it. Is it possible this might be due to the way I implemented my api requests?

Other weird thing is that temporary_assigns will not work either…i get an @saves not available in eex template error. I need this to work b/c plan is to append additional items to list w/ “load more” button using phx-update.

Any help appreciated. been digging at this all day and getting nowhere. Also, if there’s a better/cleaner way do this please let me know. a bit green.

def mount(params, _session, socket) do
      case Reddit.request_token(params["code"]) do
        "invalid_grant" ->
          {:ok,  
            socket
            |> put_flash(:error, "Token Invalid!")
            |> redirect(to: "/")
          }

        token ->
          {:ok, response} = Reddit.get_user_saves(token)
          {:ok, assign(socket, :saves, response.body)}
      end
end

Showing Posts 1 to 7

jhefreyzz

jhefreyzz

Hi, have you tried using connected? function since liveview is mounted twice.

def mount(params, _session, socket) do
  if connected?(socket) do
    case Reddit.request_token(params["code"]) do
     "invalid_grant" ->
       {:ok,  
         socket
         |> put_flash(:error, "Token Invalid!")
         |> redirect(to: "/")
       }

      token ->
       {:ok, response} = Reddit.get_user_saves(token)
       {:ok, assign(socket, :saves, response.body)}
    end
  end
end
the_dude

the_dude OP

I have but, still doesn’t work correctly.

Qqwy

Qqwy

TypeCheck Core Team

Could you maybe share what kind of logging messages are shown in the console that is running Phoenix while someone connects to it?

That would make it maybe easier to help with debugging. Even better would be if you could create a minimal example application that has the same problem, but of course that might take some more effort.

the_dude

the_dude OP

The logging is just reddit returning a json object w/ token: “invalid_grant” instead of the auth code. This is happening b/c the initial request for the token is sent w/ a one-time use code. so on the repeat request it’s returning “invalid grant” for the token.

behavior of the app is basically, initial request occurs and i see the data load on the page as it should and then within a second it redirects to my homepage based on the first case b/c it’s trying to request a token again w/ the expired code.

i am going to try this w/ another api that just needs api key and not oauth authorization and see if it causes the same issue or not.

I have implemented previously not using liveview and had no issues.

egze

egze

Your mount code will always run twice. See the docs about LiveView lifecycle. The advice about is_connected? was on the right track I think.

the_dude

the_dude OP

Tried running the reddit api code inside connected? again. it will run the request only once but it does not update the page w/ data from the request. IO.inspect confirms the request data being grabbed though.

I also built a quick liveview using another api(one that doesn’t require a key or auth and I do not have a problem at all. makes one request, loads the data on the page and that’s it. did not need to check if connected first or anything. HOWEVER, if i run this inside connected? conditional it does the same behavior as reddit api above. does not update page w/ data from request…

Looking at the docs it seems like all my request logic should be in handle_info and that would get triggered by the connected? conditional and then should update the page… Tried this too and technically it does update the data but, it also loops requests. maybe i am triggering it incorrectly though.

I’m wondering if reddit api request is doing weird stuff since it’s doing a token request then an auth/data request and then redirecting back to my uri. and this is maybe triggering renders? Maybe I need to bury the call for the token request inside the api wrapper?

the_dude

the_dude OP

Wanted to give an update on this:

I moved the case for the token validation into the api wrapper so there was only one call inside mount/3. Still caused the same problems. So I ended up just putting the api request in a regular controller then, called live_render/3 and passed my data through to the liveview w/ session%{}.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews