简体   繁体   English

如何在redux saga中获取具有不同参数的端点

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

So i had end point function所以我有终点功能

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

edited已编辑

i try to call that end point with array as a list of parameter in redux-saga我尝试使用数组调用该端点作为 redux-saga 中的参数列表

i already try using map function like in redux-saga: How to create multiple calls/side-effects programmatically for yield?我已经尝试在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))
  }

but the response is undefine, i want to know how to do multiple call with different parameter.但响应未定义,我想知道如何使用不同的参数进行多次调用。

api.listAssignedDevices has to be a function that takes vehicleId as parameter, performs network request and returns the result. api.listAssignedDevices必须是一个以vehicleId为参数,执行网络请求并返回结果的函数。 From your example it is just a function that constructs a url from vehicleId but doesn't perform network request从您的示例来看,它只是一个从 VehicleId 构造 url 但不执行网络请求的函数

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

I solve my question by doing yield all我通过做 yield all 来解决我的问题

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

from this由此

https://github.com/redux-saga/redux-saga/issues/1800#issuecomment-468627409 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