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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews