2016-02-28 31 views
8

我正在嘗試設置用戶模型的某些屬性for-each循環,但我不斷收到以下錯誤內不能調用遠程函數內部匹配:Foreach循環

不能調用遠程功能x.token/0內匹配 (elixir)src/elixir_fn.erl:9:匿名fn/3 in:elixir_fn.translate/3 (stdlib)lists.erl:1353::lists.mapfoldl/3 (elixir)src/elixir_fn.erl:14:elixir_fn.translate/3

方法:

Enum.each(users, fn(user) -> 
    user.token = Comeonin.Bcrypt.hashpwsalt(to_string(user.id)) 
end) 
+0

您是否導入了Comeonin.Bcrypt?如果你確實可以調用hashpwsalt,而沒有可能導致問題的前綴。 – GavinBrelstaff

+0

@GavinBrelstaff試過,仍然沒有工作 – naveen

+0

這裏https://hexdocs.pm/comeonin/Comeonin.Bcrypt.html它說有一個函數init() - 是否需要在調用hashpwsalt之前調用? – GavinBrelstaff

回答

11

這裏有幾個問題。 =運算符是匹配運算符,它不是賦值。爲了解釋錯誤,在語法上,這看起來像是一個匹配左側的函數調用,這是不允許的。

但這是除了你的實際目標的重點。如果你想要一組用戶模式,用新bcrypt信息更新,需要使用地圖功能:

users = Enum.map(users, fn %User{id: id}=user -> 
      %User{user| token: Comeonin.Bcrypt.hashpwsalt("#{id}")} 
     end) 

你要記住,一切都在藥劑是不可改變的。