如何在Phoenix中定义使用毒药库的模块

如何解决如何在Phoenix中定义使用毒药库的模块

我是Elixir和Phoenix的新手,所以这个问题可能很简单。我的Phoenix应用程序是一个API客户端,我正在尝试创建一个结构以对将从REST端点接收的数据进行建模。我基本上按照Poison GitHub页面上的小示例创建了我的模块:

defmodule Elixirserver.CurrentlyReprModule do
  @derive [Poison.Encoder]

  defstruct [:id,:time,:summary,:icon,:nearestStormdistance,:nearestStormbearing,:precipIntensity,:precipProbability,:temperature,:apparentTemperature,:dewPoint,:humidity,:pressure,:windSpeed,:windGust,:windbearing,:cloudCover,:uvIndex,:visibility,:ozone]
end

该模块位于lib/elixir_server/下(这是否是此类文件的最佳位置?)。

我的问题是当我尝试编译文件时出现此错误

(CompileError) lib/elixir_server/currently_repr_module.ex:2: module Poison.Encoder is not loaded and Could not be found

当我尝试运行iex -S mix时,我收到类似的错误

(UndefinedFunctionError) function Poison.Encoder.__using__/1 is undefined or private

毒素包含在mix.exs的依赖项中。如何解决错误

解决方法

您的问题可能与项目的结构或从中调用命令的位置有关。这是从零到凤凰的示例,说明如何获取使用Jason

进行json编码的结构
mix phx.new poison_demo --no-webpack --no-ecto
Fetch and install dependencies? [Yn] y
cd poison_demo

lib / poison_demo_web / router.ex

defmodule PoisonDemoWeb.Router do
  use PoisonDemoWeb,:router

  pipeline :api do
    plug :accepts,["json"]
  end

  scope "/api",PoisonDemoWeb do
    pipe_through :api
    get "/repr",ReprController,:show
  end
end

lib / poison_demo_web / controllers / repr_controller.ex

defmodule PoisonDemoWeb.ReprController do
  use PoisonDemoWeb,:controller

  def show(conn,_params) do
    json(conn,%CurrentlyReprModule{})
  end
end

lib / repr / repr.ex

defmodule CurrentlyReprModule do
  @moduledoc false
  
  @derive Jason.Encoder
  defstruct [:id,:time,:summary,:icon,:nearestStormDistance,:nearestStormBearing,:precipIntensity,:precipProbability,:temperature,:apparentTemperature,:dewPoint,:humidity,:pressure,:windSpeed,:windGust,:windBearing,:cloudCover,:uvIndex,:visibility,:ozone]
end
iex -S mix phx.server
curl localhost:4000/api/repr

{"apparentTemperature":null,"cloudCover":null,"dewPoint":null,"humidity":null,"icon":null,"id":null,"nearestStormBearing":null,"nearestStormDistance":null,"ozone":null,"precipIntensity":null,"precipProbability":null,"pressure":null,"summary":null,"temperature":null,"time":null,"uvIndex":null,"visibility":null,"windBearing":null,"windGust":null,"windSpeed":null}
,

因此,事实证明我的问题是使用模块别名。这是我之前的elixir_server.ex文件:

def repr_module do
    quote do
      use ElixirServer.CurrentlyReprModule
    end
  end

  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__,which,[])
  end

在我alias完成模块后:

def repr_module do
    quote do
      alias ElixirServer.CurrentlyReprModule,as: CurrentlyReprModule
    end
  end

  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__,[])
  end

仍不确定为什么能解决Poison错误,但我们还是接受这些。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?