Query issue in plug

Hi:
I’m new to Phoenix and currently having below issue:
I’m trying to create a plug to hook to one of my controllers, but no matter in which way I try to build an Ecto query, responds back with the same error: expected a keyword list in `where`, got: `[{"uid", "my_uid"}]`

This is the extract of the code in my plug:

def call(conn, repo) do
  user = if ["dashboard", uid] = conn.path_info do
      repo.one(from u in User, where: u.uid == ^uid) #attempt 1
      repo.get_by!(User, %{uid: uid}) #attempt 2
      repo.get_by!(User, uid: uid) #attempt 3
    end

    assign(conn, :current_user, user)
end

What am I doing wrong?

For something to be a keyword list, its key has to be an atom, not string.

1 Like

Yes, it makes sense, but, as far as i can tell, a map in the format %{key: "value"} means it’s hey is an atom, not a string, right?

Yes, but a map isn’t a keyword list either.

And I’m not even sure which of the lines would actually be able to trigger the error. Can you please show the full code and the full error message that triggers the error message? Please do not obfuscate anything and show the code as it generates the message.

2 Likes

Hi:
Thanks for the replies, but I ended up changing the query format as follows and it works just fine:

cond do
      Map.get(conn.assigns, :current_user) ->
        conn
      Enum.member?(conn.path_info, "dashboard") && length(conn.path_info) == 2  ->
        ["dashboard", uid] = conn.path_info
        user = repo.get_by(User, uid: uid)
        assign(conn, :current_user, user)
      true ->
        conn
    end