如何通过管道运算符 return 导致最后一行?

How to return result in last line by pipe operator?

我可以在最后一行 return time_stamp 中不将其提取到大括号中吗?

{:ok, time_stamp} = Myapp.Repo.insert(changeset) # |> Map.get time_stamp
time_stamp

# {:ok, %Myapp.TimeStamp{__meta__: #Ecto.Schema.Metadata<:loaded>, active: true...}

您可以使用 Kernel.elem/2 通过索引(从 0 开始)从元组中获取项目。

Myapp.Repo.insert(changeset) |> elem(1)

但是,如果您的变更集无效,那么这将 return 变更集,因为无效的变更集将 return {error, changeset}

如果您能稍微解释一下为什么要这样做,那么我们可能会提供进一步的帮助。