小编D.R*_*D.R的帖子

Ecto/Elixir,我如何按日期查询?

我正在处理我的应用程序的统计页面,并尝试按日期查询数据.

为了获得日期范围,我使用 Calendar.Date

date_range = Date.days_after_until(start_date, end_date, true)
|> Enum.to_list
Run Code Online (Sandbox Code Playgroud)

它返回日期的日期列表,每个日期看起来像"2017-04-07".因此,根据我的日期date_range,我尝试查询,但它会触发如下错误.

where cannot be cast to type Ecto.DateTime in query: from o in Myapp.Order, where: o.created_date >= ^~D[2017-04-07]

对于created_date秩序领域,我做了这样的领域, field :created_date, Ecto.DateTime.

如果我想按日期查询,我该如何查询?

预先感谢.

datetime elixir ecto

14
推荐指数
1
解决办法
5557
查看次数

如何过滤反应中的状态数组?

我有订单减速机,它有很多州.

const initialState = {
 channel: null,
 order: {},
 fetching:true,
 menu: [],
 categories: [],
 subcategories: [],
 currentCategoryId: 1,
 currentSubcategoryId: 5,
 currentMenu: [],
};
Run Code Online (Sandbox Code Playgroud)

我想要过滤的是menu.menu是状态的阵列,其具有对象menu_item我有currentCategoryIdcurrentSubcategoryId.我想对这些状态做的是通过使用currentCategoryIdcurrentSubcategoryId过滤menu并将过滤后的状态放到currentMenu.

case Constants.ORDER_CHANNEL_CONNECTED:
 return
  {...state,currentMenu: action.menu.map((menu) => {
                    if(state.currentCategoryId == menu.category_id){
                        return menu;
                    }
                    else return false;}}
Run Code Online (Sandbox Code Playgroud)

所以要做到这一点,我做了上面的代码.即使它返回一些过滤值,它也会显示具有许多错误值的相同数量的数组.我想找到其他方法来做到这一点..

我怎样才能做到这一点?

提前致谢.

filter reactjs redux

4
推荐指数
1
解决办法
4万
查看次数

react js mapStateToProps 触发未捕获的类型错误:无法读取未定义的属性“地图”

我有一个 index.js 呈现数据库中的所有表。

_renderAllTables() {
const { fetching } = this.props;

let content = false;

if(!fetching) {
  content = (
    <div className="tables-wrapper">
      {::this._renderTables(this.props.allTables)}
    </div>
    );
}

return (
  <section>
    <header className="view-header">
      <h3>All Tables</h3>
    </header>
    {content}
  </section>
);
}

_renderTables(tables) {

return tables.map((table) => {
  return <Table
            key={table.id}
            dispatch={this.props.dispatch}
            {...table} />;               
});
}

render() {
return (
  <div className="view-container tables index">
    {::this._renderAllTables()}
  </div>
  );
 }
}
const mapStateToProps = (state) => (
  state.tables);

export default connect(mapStateToProps)(HomeIndexView);
Run Code Online (Sandbox Code Playgroud)

我将 mapStateToProps 从上面的代码更改为下面的代码。

 const mapStateToProps …
Run Code Online (Sandbox Code Playgroud)

reactjs redux

2
推荐指数
1
解决办法
1万
查看次数

如何使用for循环在react组件中列出整数?

我有一个名为rounds和 prop 的状态current_round。current_round 的默认值为 0。我想用这些值做的是用整数列出按钮。例如,如果 的值为rounds4,则列出按钮 1、2、3 和 4。

for (var i =(this.props.order.order.current_round + 1); i<(this.props.order.rounds+1); i++) {

  return (
    <li><button id ={i} class="rounds_btn">{i}</button></li>
    );
  }
Run Code Online (Sandbox Code Playgroud)

为此,我像上面一样制作了 for 循环,但它只显示一个整数 1 的按钮..

我的代码有什么问题吗?它没有显示任何错误。

提前致谢

javascript for-loop reactjs

2
推荐指数
1
解决办法
7386
查看次数

HTTPoison 在标头中使用 Authorization 发出 post 请求。

我正在尝试使用HTTPoison.

我想传递带有标题的 json 数据,其中包含"Authorization": Bearer #{token}.

为了实现这一点,我已经尝试过,

headers = [{"Authorization": "Bearer #{token}"}, {"Content-Type", "application/json"}] 
body = 
 %{
  id: id,
  name: name,
  ...      
 }
HTTPoison.post(url, body, headers)
Run Code Online (Sandbox Code Playgroud)

但它会触发一个语法错误,即syntax error before: "Authorization". 我一直在寻找正确的标题语法,但仍然没有运气..

什么是正确的语法headers

提前致谢..

header elixir httpoison

2
推荐指数
1
解决办法
3119
查看次数

如何存储和呈现时间状态?(YYYY-MM-DD 格式)在反应

在我的Statistics州,有

const initialState = {
 fetching: false,
 channel: null,
 dateFrom: {},
 dateTo: {},
};
Run Code Online (Sandbox Code Playgroud)

dateFromdateTo是当用户点击日历中的开始日期和结束日期时的值。我rc-calendar用于显示和获取日期数据。

在我的GeneralReport组件中,我试图显示 start datedateFrom和 end date dateTo。所以我做了如下代码

render () {
    const { stat } = this.props;
    console.log(this.props.stat)
    return (
        <div className = "general_report">
            <header className = "general_report_head">General Report</header>
            <div className="report_dates">
            From<span className="number">{this.props.stat.dateFrom}</span>
Run Code Online (Sandbox Code Playgroud)

到{this.props.stat.dateTo}

但它触发了错误,Objects are not valid as a React child (found: Thu Jan 12 2017 08:00:00 GMT+0800 (HKT)). If you meant to …

state date reactjs

1
推荐指数
1
解决办法
2万
查看次数

POST时的Phoenix.ActionClauseError,对Myapp.StatisticsController.calculate的错误请求

我有一个Statistics控制器,它获取date_fromdate_to计算之间进行收入的订单数量和金额date_fromdate_to.

def calculate(conn,  %{"statistics" => %{"date_from" => date_from, "date_to" => date_to}}) do

revenue = Repo.one(from p in Order, where: p.created_date >= ^date_from and p.created_date <= ^date_to, select: sum(p.paied))
order = Repo.one(from p in Order, where: p.created_date >= ^date_from and p.created_date <= ^date_to, select: count(p.id))

conn
|> put_resp_content_type("application/json")
|> render("statistics.json", order: order, revenue: revenue)

end
Run Code Online (Sandbox Code Playgroud)

以下是statistics.view和router,它会导致错误.

  def render("statistics.json", %{order: order, revenue: revenue}) do

%{
  order: order

}

%{
  revenue: revenue
} …
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework

1
推荐指数
1
解决办法
784
查看次数

如何在System.cmd中使用管道?

我有一个功能,可以获取所有USB连接设备的信息。

connected_devices = :os.cmd('usb-devices | grep -A 1 Product=')

当我使用时:os.cmd,它工作正常。但是,由于:os.cmd不返回错误代码,因此我想使用它System.cmd来更好地处理错误。

connected_deivces = System.cmd("usb-devices", ["|", "grep", "-A", "1", "Product="])

因此,我尝试更改上述功能,但在管道之后不执行命令。

如何使用管道执行命令System.cmd

cmd elixir

1
推荐指数
1
解决办法
172
查看次数

protocol nume(phoenix-framework)无法实现

我在订单控制器中添加了一个功能"付款",并添加了一个路由.

def payment(conn, %{"id" => id}) do
  foods = Repo.all(Food)
  order =
    Repo.get!(assoc(conn.assigns[:table], :orders), id)
    |> Repo.preload(order_items: :food)
  order_item_changeset =
    order
    |> build_assoc(:order_items)
    |> Pos1.OrderItem.changeset()
  order_changeset = Order.changeset(order)

  render(conn, "payment.html", order: order, order_item_changeset: order_item_changeset, order_changeset: order_changeset, foods: foods, payment: @payment)
end

resources "/tables", TableController do
  resources "/orders", OrderController do
    get "/payment", OrderController, :payment
end
Run Code Online (Sandbox Code Playgroud)

我在表和订单下添加了付款路线,因为付款必须显示其表号和订单ID.我在show.html的订单模板中添加了指向付款页面的链接.但是,它会导致Protocol.UndefinedError.

[error] #PID<0.9096.0> running Pos1.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /tables/1/orders/14
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Enumerable not implemented for nil …
Run Code Online (Sandbox Code Playgroud)

elixir phoenix-framework

0
推荐指数
1
解决办法
3537
查看次数

(Postgrex.Error)错误 42P18(不确定数据类型),当它尝试查询时。

我有一个查询函数,它给出utc_date_fromutc_date_to并取 的和total

由于我需要提供本地日期,因此我将time_period本地日期添加到 中select

time_period = to_string(date)
Payment
|> where([p], p.core_id == ^core_id and
              p.inserted_at >= datetime_add(^utc_date_from, 0, "day") and
              p.inserted_at <= datetime_add(^utc_date_to, 0, "day"))
|> select([p], %{amount: sum(p.total), time_period: ^time_period})
|> Repo.one()
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加time_periodselect,它会触发一个错误(Postgrex.Error) ERROR 42P18 (indeterminate_datatype): could not determine data type of parameter $1

此外,此错误仅在我在 ubuntu 中运行我的应用程序时显示。如果我尝试使用 mac os,它不会显示此错误。

这个函数有什么问题吗?我怎样才能让它在ubuntu上工作?

postgresql elixir ecto

0
推荐指数
1
解决办法
1271
查看次数