0
votes

I want to know why am I getting this error and how to fix. Could it be due to user id conflict in database? Another thing is this might not even be the file which is giving out this error.

enter image description here

defmodule MessengyrWeb.UserView do
  use MessengyrWeb, :view

  def render("show.json", %{user: user}) do
    %{
        user: user_json(user)
    }
  end

  def user_json(user) do
    hash_email = :crypto.hash(:md5, user.email) |> Base.encode16 |> String.downcase
    avatar_url = "http://www.gravatar.com/avatar/#{hash_email}"

    %{
        id: user.id,
        username: user.username,
        avatarURL: avatar_url,
    }
  end
end

This is what I get after commenting out id: user.id and user: user.username and replacing hash_email with a static integer.

enter image description here

def user_json(user) do
    hash_email = 123
    avatar_url = "http://www.gravatar.com/avatar/#{hash_email}"

    %{
        # id: user.id,
        # username: user.username,
        avatarURL: avatar_url,
    }
end
2
Your user variable has a nil instead of a user struct. We would have to see how you are retrieving the user to tell you why this is happening. But chances are you are calling Attendance.Repo.get with an id that does not exist.Justin Wood
I got it. Thanks! @JustinWoodSaw Thinkar Nay Htoo

2 Answers

1
votes

I deleted the whole database and run mix ecto.create && mix ecto.migrateagain.

And mix phx.server and created a new user account.

Also tested with a different browser it works. So I cleared cookies of the former browser and it works now.

0
votes

I did mix ecto.reset and it worked for me.