2

我正在实现一个 react/redux 应用程序,我在其中调用 API 来获取一些数据。API 用 F# 编写并使用 Suave.io。在我的后端,对于一些 API 调用,我返回一个 DataSet(.NET 类型)。它包括许多具有多列的数据表。这些列的名称来自数据库。当我从 API 获取数据时,Suave.io 将 mime 类型设置为 JSON,因此它需要 DataSets 并将它们作为 json 对象传递给视图。除了列名设置为数据库名称的数据表之外,该视图具有正确的所有数据。例如,如果名称是“IND.APPL”,那么在视图中它将是“inD.APPL”。我不知道为什么会这样。

调用后端获取数据:

export function loadIndicesDataAPI(data) {
    return function(dispatch) {
        dispatch(beginAjaxCall());
        return fetch(`http://localhost:8083/indices`, {
            method: 'post',
            header: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
        })
        .then(response => {
            return response.json();
        })
        .then(dataRes => {
            dispatch(loadIndicesDataSuccess(dataRes));
        }).catch(error => {
            dispatch(ajaxCallError(error))
            throw(error);
        });
    };
}

Suave API 代码片段:

type indicesForm = JsonProvider<""" { "data": [{ "shortName": "s", "name": "n", "symbolId": 1, "isAdded": true, "isDatabase": true, "isReturn": false }], "startdate": "2010-01-01", "enddate": "2011-01-01", "rebalance": 0, "observationAnalysis": 1 } """>

[<AutoOpen>]
module RestFul =

  type RestInit = {
    RefreshAPI : IndexItem [] * DateTime * DateTime * int * int -> DataSet
  } 

let JSON v =
  let jsonSerializerSettings = new JsonSerializerSettings()
  jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()

  JsonConvert.SerializeObject(v, jsonSerializerSettings)
  |> OK
  >=> Writers.setMimeType "application/json; charset=utf-8"

let fromJson<'a> json =
  let indicesFormJson = indicesForm.Parse(json)
  let indexItems = Array.init (indicesFormJson.Data.Length) (fun ind -> 
    let data = indicesFormJson.Data.[ind]
    let indexItemNew = new IndexItem(data.SymbolId, data.Name, data.ShortName, data.IsReturn)
    if data.IsAdded then indexItemNew.Add()
    if data.IsDatabase then indexItemNew.Imported()
    indexItemNew)
  let startDate = indicesFormJson.Startdate
  let endDate = indicesFormJson.Enddate
  let rebalance = indicesFormJson.Rebalance
  let observationAnalysis = indicesFormJson.ObservationAnalysis
  indexItems, startDate, endDate, rebalance, observationAnalysis

let getResourceFromReq<'a> (req : HttpRequest) =
  let getString rawForm =
    System.Text.Encoding.UTF8.GetString(rawForm)
  req.rawForm |> getString |> fromJson<'a>

let restInit resourceName resource =
  let resourcePath = "/" + resourceName
  path resourcePath >=> choose [
    POST >=> request (getResourceFromReq >> resource.RefreshAPI >> JSON)
  ]


[<EntryPoint>]
let main argv =
  let indicesWebPart = restInit "indices" {
    RestInit.RefreshAPI = RefreshAPI 
  }

  startWebServer defaultConfig (choose [indicesWebPart])
  0

另一件事是列名以大写字母开头,并且在前端变为小写。

4

1 回答 1

1

我认为这里有几个问题......有点......而且我认为 react/redux不是像你那样提取数据,而是推送数据。

除此之外:我猜:

jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()

是相关的。

因此,通过阅读http://www.newtonsoft.com/json/help/html/contractresolver.htm的文档,我认为可以选择 CamelCasePropertyNamesContractResolver 以外的其他方法,或者实际上甚至覆盖创建的命名。

以上假设问题是:“为什么命名是根据我没有费心阅读文档的内容,还有我错过了什么?还是什么?”

于 2016-09-07T05:49:58.053 回答