[英]Elixir app working in iex but fail when try to mix run
I am trying to create application with Elixir, Ecto without Pheonix.我正在尝试使用没有 Pheonix 的 Elixir、Ecto 创建应用程序。
mix.exs mix.exs
defmodule Wtf.MixProject do
use Mix.Project
def project do
[
app: :wtf,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
mod: {Wtf.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:postgrex, "~> 0.16.3"},
{:ecto, "~> 3.8.4"},
{:ecto_sql, "~> 3.8.3"},
{:json, "~> 1.4"},
{:jason, "~> 1.3.0"},
{:ratatouille, "~> 0.5.0"},
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
repo.ex回购协议
defmodule Wtf.Repo do
use Ecto.Repo,
otp_app: :wtf,
adapter: Ecto.Adapters.Postgres
end
application.ex应用程序.ex
defmodule Wtf.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: Wtf.Worker.start_link(arg)
# {Wtf.Worker, arg}
Wtf.Repo
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Wtf.Supervisor]
Supervisor.start_link(children, opts)
end
end
config.exs配置文件
import Config
config :wtf, Wtf.Repo,
database: "wtf_database",
username: "wtf",
password: "wtf",
hostname: "localhost",
port: "5050",
log: false
config :wtf, ecto_repos: [Wtf.Repo]
wtf.ex wtf.ex
defmodule Wtf do
import Ecto.Query, warn: false
alias Wtf.{Repo, Data}
def hello do
Repo.all(WtfData)
end
end
Below I am attaching list of my files in project like you can see that is just basic setup for quering database.下面我附上了我在项目中的文件列表,就像您看到的那样,这只是查询数据库的基本设置。 When I run
iex -S mix
app loading properly and I can query database all CRUD working fine, when I do Wtf.Repo.all(Wtf.Data)
I am getting all 3 rwos which I have in database.当我运行
iex -S mix
应用程序加载正确并且我可以查询数据库所有 CRUD 工作正常时,当我执行Wtf.Repo.all(Wtf.Data)
时,我得到了数据库中的所有 3 个 rwo。 But when I am trying to run this app with mix run lib/wtf.ex
I am getting error function Repo.all/1 is undefined (module Repo is not abvailable)
I was looking in the google how to solve it and it seems like I am doing everything like it should be done.但是当我尝试使用
mix run lib/wtf.ex
运行这个应用程序时,我得到了错误function Repo.all/1 is undefined (module Repo is not abvailable)
我在谷歌中寻找如何解决它,看起来像我正在做所有应该做的事情。 I follow few examples but always the same issue.我遵循几个例子,但总是同样的问题。 Can some explain me what is wrong with it?
有人可以解释一下它有什么问题吗? What am I missing?
我错过了什么?
Sidenote: of course you don't need to build a release unless you want to run a release.旁注:当然,除非您想运行发布,否则您不需要构建发布。
The below part of mix.exs
defines the application within your project and its entry point. mix.exs
的以下部分定义了项目中的应用程序及其入口点。
def application do
[
extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ THIS
mod: {Wtf.Application, []}
]
end
Instead of running an explicit file with mix run ‹file›
, do mix run
without a parameter, which is, according to docs,不要使用
mix run ‹file›
运行显式文件,而是在没有参数的情况下进行mix run
,根据文档,
can be used to start the current application dependencies, the application itself, and optionally run some code in its context.
可用于启动当前应用程序依赖项、应用程序本身,并可选择在其上下文中运行一些代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.