我想將花庫(/api/tasks)的回傳映射到物件串列。目前它回傳多個物件,但沒有“串列包裝器”,因此無法對其進行迭代。 API: https ://flower.readthedocs.io/en/latest/api.html#get--api-tasks
回報例如:
HTTP/1.1 200 OK
Content-Length: 1109
Content-Type: application/json; charset=UTF-8
Etag: "b2478118015c8b825f7b88ce6b660e5449746c37"
Server: TornadoServer/3.1.1
{
"e42ceb2d-8730-47b5-8b4d-8e0d2a1ef7c9": {
"args": "[3, 4]",
"client": null,
"clock": 1079,
"eta": null,
"exception": null,
"exchange": null,
"expires": null,
"failed": null,
"kwargs": "{}",
"name": "tasks.add",
"received": 1398505411.107885,
"result": "'7'",
"retried": null,
"retries": 0,
"revoked": null,
"routing_key": null,
"runtime": 0.01610181899741292,
"sent": null,
"started": 1398505411.108985,
"state": "SUCCESS",
"succeeded": 1398505411.124802,
"timestamp": 1398505411.124802,
"traceback": null,
"uuid": "e42ceb2d-8730-47b5-8b4d-8e0d2a1ef7c9",
"worker": "celery@worker1"
},
"f67ea225-ae9e-42a8-90b0-5de0b24507e0": {
"args": "[1, 2]",
"client": null,
"clock": 1042,
"eta": null,
"exception": null,
"exchange": null,
"expires": null,
"failed": null,
"kwargs": "{}",
"name": "tasks.add",
"received": 1398505395.327208,
"result": "'3'",
"retried": null,
"retries": 0,
"revoked": null,
"routing_key": null,
"runtime": 0.012884548006695695,
"sent": null,
"started": 1398505395.3289,
"state": "SUCCESS",
"succeeded": 1398505395.341089,
"timestamp": 1398505395.341089,
"traceback": null,
"uuid": "f67ea225-ae9e-42a8-90b0-5de0b24507e0",
"worker": "celery@worker1"
}
}
任何想法如何做到這一點?我已經嘗試過以下事情:
export interface Tasks {
tasks: TaskWrapper[]
}
export interface TaskWrapper {
[uuid: string]: Task
}
export interface Task {
uuid: string,
state: string,
received: string,
}
添加 Dragan 的示例會導致以下問題:
loadAllTasksFromFlower(): Observable<Task[]> {
return this.http.get<Task[]>("localhost:5566/api/tasks")
.pipe(map(response => Object.entries(response)
.map(entry => ({ uuid: entry[0], state: entry[1].state, received: entry[1].received }))
}
TS2322:型別 'Observable<{ uuid: string; 狀態:任務狀態;收到:任何;}[]>' 不可分配給型別 'Observable<Task[]>'。鍵入'{ uuid:字串;狀態:任務狀態;收到:任何;}[]' 不可分配給型別 'Task[]'。鍵入'{ uuid:字串;狀態:任務狀態;收到:任何;}' 缺少型別“任務”的以下屬性:型別、源、呼叫、回呼和另外 3 個。
uj5u.com熱心網友回復:
您組織介面的方式很好,您可以通過這種方式將專案放入陣列中:
const tasks: Task[] = Object.entries(data).map(entry => ({
uuid: entry[0],
state: entry[1].state,
received: entry[1].received
}));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532565.html
