我刚刚完成了Elixir课程,现在我正在修改运行一个基于Phoenix 1.3的项目,项目编译得很好,本地服务器启动没有任何错误,但是当我尝试打开我收到错误的链接:
以下是account.ex文件的代码:
defmodule App.Account do
alias App.Repo
alias Xarango.Query
alias App.Account.User
def list_users do
User.list(%{})
end
def get_user!(id) do
try do
{:ok, User.one(%{_key: id})}
rescue
e in Xarango.Error ->
{:error, :not_found}
end
end
def get_user_by_attr(attr) do
try do
{:ok, User.one(attr)}
rescue
Xarango.Error ->
key = attr
|> Map.keys
|> List.first
|> Atom.to_string
{:error, "#{key} not found"}
end
end
def check_credentials(%{"password" => password, "email" => email}) do
with {:ok, user} <- get_user_by_attr(%{email: email}) do
if password = user.doc._data.password do
{:ok, user}
else
{:error, "Incorrect password"}
end
end
end
def create_user(%{"email" => email, "nick" => nick} = attrs, roles) when is_list(roles) do
with {:ok, _resp} <- check_if_attr_exists(%{email: email}),
{:ok, _resp} <- check_if_attr_exists(%{nick: nick}),
{:ok, user} <- get_avatar(attrs) do
{:ok, user |> Map.put("roles", roles) |> User.create}
end
end
defp get_avatar(%{"email" => email} = attrs) do
hash_email = :crypto.hash(:md5, email) |> Base.encode16
{:ok, Map.put(attrs, "avatar", "https://api.adorable.io/avatars/100/#{hash_email}")}
end
defp check_if_attr_exists(attr) do
try do
User.one(attr)
key = attr
|> Map.keys
|> List.first
|> Atom.to_string
{:error, "#{key} already exists"}
rescue
Xarango.Error ->
{:ok, :continue}
end
end
def hash_password(%{"password" => password} = attrs) do
with {:ok} <- valid_password?(password),
%{password: _, password_hash: password} <- password do
{:ok, Map.put(attrs, "password", password)}
end
end
defp valid_password?(password) when byte_size(password) > 6 do
{:ok}
end
defp valid_password?(_), do: {:error, "The password is to short"}
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end
def delete_user(%User{} = user) do
Repo.delete(user)
end
def change_user(%User{} = user) do
User.changeset(user, %{})
end
alias App.Account.Client
Query.query(q).result
end
def get_client!(id) do
q = """
FOR c IN clients
FILTER c._key == #{id}
RETURN c
"""
List.first Query.query(q).result
end
def create_client(attrs \\ %{}) do
%Client{}
|> Client.changeset(attrs)
|> Repo.insert()
end
def update_client(%Client{} = client, attrs) do
client
|> Client.changeset(attrs)
|> Repo.update()
end
def delete_client(%Client{} = client) do
Repo.delete(client)
end
def change_client(%Client{} = client) do
Client.changeset(client, %{})
end
end
这是我的client.ex文件:
defmodule App.Account.Client do
use Xarango.Domain.Document, collection: :clients
end
以下是我的ticket_controller.ex文件的一部分:
defmodule AppWeb.TicketController do
use AppWeb, :controller
alias App.Support
alias App.Support.Ticket
action_fallback AppWeb.FallbackController
def index(conn, _params) do
IO.inspect(Support.create_ticket())
tickets = Support.list_tickets()
render(conn, "index.json", tickets: tickets)
end
更新我在这里也提供了我的ticket.ex文件的代码(App.Support.Ticket):
defmodule App.Support.Ticket do
use Xarango.Domain.Document, collection: :helpdesk_tickets
end
有什么想法吗?有人可以解释一下我到底做错了什么吗?
更新2:我在文件夹/lib/app/repo.ex中添加了一个文件repo.ex,这是我从工作项目中获取的:
defmodule App.Repo do
use Ecto.Repo, otp_app: :app
@doc """
Dynamically loads the repository url from the
DATABASE_URL environment variable.
"""
def init(_, opts) do
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
end
end
我还修复了ecto的一个问题:
(CompileError) lib/app/repo.ex:2: module Ecto.Repo is not loaded and could not be found
(elixir) expanding macro: Kernel.use/2
lib/app/repo.ex:2: App.Repo (module)
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
我通过添加{:phoenix_ecto,&#34;〜&gt;来修复它3.2&#34;}使用mix.exs和runnig&#34;&gt;混合deps.get&#34;。 现在我有一个新错误:
== Compilation error in file lib/app/repo.ex ==
** (ArgumentError) missing :adapter configuration in config :app, App.Repo
lib/ecto/repo/supervisor.ex:70: Ecto.Repo.Supervisor.compile_config/2
lib/app/repo.ex:2: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
有人可以建议我现在应该解决什么问题吗?我觉得这个错误出现了,因为我从连接到PostgresDB的项目复制了repo.ex文件,而我当前的项目连接到ArangoDB。