【问题标题】:Error in Elixir : returned a bad value: :okElixir 中的错误:返回一个错误的值::ok
【发布时间】:2018-04-25 02:47:30
【问题描述】:

我在开发 Elixir 时遇到了一些问题。 这是我的mix.exs...

defmodule FlockTest.Mixfile do
  use Mix.Project

  def project do
    [app: :flock_test,
     version: "0.1.0",
     elixir: "~> 1.4",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps()]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    # Specify extra applications you'll use from Erlang/Elixir
    [
      extra_applications: [:logger],
      mod: {FlockTest, []}
    ]
  end
...
...

这是FlockTest的代码。

defmodule FlockTest do
  @moduledoc """
  Documentation for FlockTest.
  """

  def start(_type, _args) do
    IO.puts "Start Flock"
  end
end

我使用mix run --no-halt 运行此代码,但这会导致这样的错误。

=INFO REPORT==== 12-Nov-2017::17:47:39 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application flock_test: FlockTest.start(:normal,     
[]) returned a bad value: :ok

我做错了吗?

【问题讨论】:

    标签: elixir elixir-mix


    【解决方案1】:

    您的应用程序的start/2 函数应该启动并返回应用程序的顶级进程,通常是主管实例。现在你返回IO.puts 返回的值,成功时为:ok。有效的返回值为{:ok, pid} | {:ok, pid, state} | {:error, reason :: term},如文档here 所述。例如,您可以使用mix new foo --sup 创建一个新应用程序并查看lib/foo/application.ex。下面是它的start/2 的样子:

    def start(_type, _args) do
      # List all child processes to be supervised
      children = [
        # Starts a worker by calling: A.Worker.start_link(arg)
        # {A.Worker, arg},
      ]
    
      # See https://hexdocs.pm/elixir/Supervisor.html
      # for other strategies and supported options
      opts = [strategy: :one_for_one, name: A.Supervisor]
      Supervisor.start_link(children, opts)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      相关资源
      最近更新 更多