简体   繁体   English

在erlang视图中发出嵌套哈希值

[英]emitting value of nested hash in erlang view

i try to emit the postal_country in my view and i have no idea how this would be done in erlang. 我试图在我看来发出postal_country ,我不知道该如何在erlang中完成。

this is the structure of my document: 这是我的文档的结构:

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

this is the js i want to translate: 这是我要翻译的js:

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

this is what i tried in erlang: 这是我在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.

You need to unwrap Address as suggested at https://stackoverflow.com/a/2422631/453605 : 您需要按照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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM