簡體   English   中英

如何在redux saga中獲取具有不同參數的端點

[英]how to fetch end point with different parameters in redux saga

所以我有終點功能

import AppConfig from "../config/app-config";

const create = (baseURL = AppConfig.apiUrl) => {
  const api = apisauce.create({
    baseURL,
    headers: {
      "Cache-Control": "no-cache"
    },
    timeout: 10000
  });

const listAssignedDevices = vehicleId =>
    api.get(`api/fleet/vehicle/${vehicleId}/devices`) 

return listAssignedDevices
export default create

已編輯

我嘗試使用數組調用該端點作為 redux-saga 中的參數列表

我已經嘗試在redux-saga 中使用 map 函數:如何以編程方式創建多個調用/副作用以獲取收益?

const listVehicleId = [1,2,3,4,5,6,7]

const response = yield listVehicleId.map(vehicleId => call(api.listAssignedDevices, vehicleId)
console.log(response)

if (response.ok && response.headers['content-type'].indexOf('json') !== -1) {
    console.tron.log('AturBcak - OK')
    yield put(AturBcakActions.aturBcakMultipleSuccess(response.data))
  }

但響應未定義,我想知道如何使用不同的參數進行多次調用。

api.listAssignedDevices必須是一個以vehicleId為參數,執行網絡請求並返回結果的函數。 從您的示例來看,它只是一個從 VehicleId 構造 url 但不執行網絡請求的函數

const listAssignedDevices = vehicleId => fetch(`vehicle/${vehicleId}/devices`)

我通過做 yield all 來解決我的問題

const response = yield all(
    listVehicleId.map(vehicleId => 
      call(api.listAssignedDevices, vehicleId)  
    )
  )

由此

https://github.com/redux-saga/redux-saga/issues/1800#issuecomment-468627409

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM