2

为清楚起见进行了编辑

不知何故,这有效:

path "/" >=> warbler (fun _ -> OK (string DateTime.Now))

但是这个没有:

let txnAccA =
    let sqlStr = "select JSON from Store.Txn"
    let result = Db.execute sqlStr Config.oConnStr
    match result with
    | Some a ->
        [for i in a do 
            let msg = JsonConvert.DeserializeObject<TxnAccA>(i)
            yield msg
            ]

    | _ ->
        List.empty<TxnAccA>

let txnAmtA =
    let sqlStr = "select JSON from Store.Amt"
    let result = Db.execute sqlStr Config.oConnStr
    match result with
    | Some a ->
        [for i in a do 
            let msg = JsonConvert.DeserializeObject<TxnAmtA>(i)
            yield msg
            ]

    | _ ->
        List.empty<TxnAmtA>

let result ()= {Acc= txnAccA; Amt= txnAmtA}
path "/txn" >=> warbler (fun _ -> page "txn.html" (result()))

“作品”是指页面不是静态的,它显示来自数据库的最新数据。知道为什么吗?

4

1 回答 1

2

txnAccA并且txnAmtA需要是函数(类似于result)。它们现在被定义为值,因此被分配一次,并且不会为每个请求查询数据库。result每次调用时都会创建一条新记录,但值始终保持不变。

let txnAccA () = //...

let txnAmtA () = //...

let result () = { Acc = txnAccA(); Amt = txnAmtA() }
path "/txn" >=> warbler (fun _ -> page "txn.html" (result()))
于 2017-10-11T16:32:23.820 回答