无法从榆树端口解码会话
Can't decode session from elm port
正在尝试让 elm 端口正常工作以维持会话。
在 index.html 中,脚本包含以下侦听器:
window.addEventListener("load", function(event) {
app.ports.onSessionChange.send(localStorage.session);
}, false);
localStorage.session
看起来像这样(它会一直保留到我注销为止):
{"email":"user@fake.com","token":"eyJhbG...","user_id":1,"handle":"me"}
Ports.elm
中的定义是:
port onSessionChange : (Value -> msg) -> Sub msg
此端口连接到此处的 Main.elm
(如果我忘记包含下面的一些定义,请告诉我):
subscriptions : Model -> Sub Msg
subscriptions model =
Ports.onSessionChange sessionChange
sessionChange : Json.Decode.Value -> Msg
sessionChange value =
let
result =
Json.Decode.decodeValue sessionDecoder value
in
case result of
Ok sess ->
SetSession (Just sess)
Err err ->
SetSession Nothing
...
type alias Session =
{ email : String
, token : String
, user_id : Int
, handle : String
}
...
import Json.Decode as Decode exposing (..)
import Json.Decode.Pipeline as Pipeline exposing (decode, required)
sessionDecoder : Decode.Decoder Session
sessionDecoder =
Pipeline.decode Session
|> Pipeline.required "email" Decode.string
|> Pipeline.required "token" Decode.string
|> Pipeline.required "user_id" Decode.int
|> Pipeline.required "handle" Decode.string
...
type Msg
= NoOp
| SetSession (Maybe Session)
...
update msg model =
case msg of
SetSession session ->
case Debug.log "session = " session of
Just sess ->
({ model | session = sess } , Cmd.none)
Nothing ->
(model, Cmd.none)
Debug.log "session"
在页面加载时在控制台中显示 Nothing
,因此 JS 正在与 elm 对话,但解码器似乎失败了。有什么想法吗?
我已将您的代码插入 minimal working example,一切正常。您可能希望从 javascript 部分中记录 localStorage.session
的值,以确保它是一个有效的 JSON 值。
正在尝试让 elm 端口正常工作以维持会话。
在 index.html 中,脚本包含以下侦听器:
window.addEventListener("load", function(event) {
app.ports.onSessionChange.send(localStorage.session);
}, false);
localStorage.session
看起来像这样(它会一直保留到我注销为止):
{"email":"user@fake.com","token":"eyJhbG...","user_id":1,"handle":"me"}
Ports.elm
中的定义是:
port onSessionChange : (Value -> msg) -> Sub msg
此端口连接到此处的 Main.elm
(如果我忘记包含下面的一些定义,请告诉我):
subscriptions : Model -> Sub Msg
subscriptions model =
Ports.onSessionChange sessionChange
sessionChange : Json.Decode.Value -> Msg
sessionChange value =
let
result =
Json.Decode.decodeValue sessionDecoder value
in
case result of
Ok sess ->
SetSession (Just sess)
Err err ->
SetSession Nothing
...
type alias Session =
{ email : String
, token : String
, user_id : Int
, handle : String
}
...
import Json.Decode as Decode exposing (..)
import Json.Decode.Pipeline as Pipeline exposing (decode, required)
sessionDecoder : Decode.Decoder Session
sessionDecoder =
Pipeline.decode Session
|> Pipeline.required "email" Decode.string
|> Pipeline.required "token" Decode.string
|> Pipeline.required "user_id" Decode.int
|> Pipeline.required "handle" Decode.string
...
type Msg
= NoOp
| SetSession (Maybe Session)
...
update msg model =
case msg of
SetSession session ->
case Debug.log "session = " session of
Just sess ->
({ model | session = sess } , Cmd.none)
Nothing ->
(model, Cmd.none)
Debug.log "session"
在页面加载时在控制台中显示 Nothing
,因此 JS 正在与 elm 对话,但解码器似乎失败了。有什么想法吗?
我已将您的代码插入 minimal working example,一切正常。您可能希望从 javascript 部分中记录 localStorage.session
的值,以确保它是一个有效的 JSON 值。