【问题标题】:emitting value of nested hash in erlang view在erlang视图中发出嵌套哈希值
【发布时间】:2012-03-02 00:35:15
【问题描述】:

在我看来,我尝试发出 postal_country,但我不知道如何在 erlang 中完成。

这是我的文档的结构:

{
   ...
   "postal_address": {
       "postal_country": "BE"
       ...
   }
}

这是我要翻译的js:

function(doc) {
  if (doc['ruby_class'] == 'Company' && doc['postal_address']['postal_country']) {
    emit(doc['postal_address']['postal_country'], 1)
  }
}

这是我在 erlang 中尝试过的:

fun({Doc}) ->
  case proplists:get_value(<<"ruby_class">>, Doc) of
    <<"Company">> ->
      Addr = proplists:get_value(<<"postal_address">>, Doc, null), 
      Key = proplists:get_value(<<"postal_country">>, Addr, null),
      Emit(Key, 1);
    _ ->
    ok
  end
end.

【问题讨论】:

  • 这个函数有错误吗? Emit 似乎是一个未绑定的变量...
  • (仅供参考 @Isac Emit 是 CouchDB 的一个函数:他正在编写一个 CouchDB 映射函数)

标签: erlang couchdb


【解决方案1】:

您需要按照https://stackoverflow.com/a/2422631/453605 的建议解开地址:

fun({Doc}) ->
  case proplists:get_value(<<"ruby_class">>, Doc) of
    <<"Company">> ->
      case proplists:get_value(<<"postal_address">>, Doc) of
        {Address} ->
          Country = proplists:get_value(<<"postal_country">>, Address),
          Emit(Country, 1);
        _ ->
          ok
      end;
    _ ->
      ok
  end
end.

【讨论】:

  • 看起来很有希望。现在视图生成与** Reason for termination == ** {{badmatch,null},[{erl_eval,expr,3,[]}]} 崩溃至少我认为这是相关的错误。我需要以某种方式检查 null 吗? erlang 堆栈对我并没有真正的帮助......
  • 我已更新示例以说明doc['postal_address'] == null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多