代码之家  ›  专栏  ›  技术社区  ›  Michael Paccione

Redux Saga如何解析数据

  •  0
  • Michael Paccione  · 技术社区  · 6 年前

    你好,第一次与发电机或Redux Sagas合作。我需要分析这些数据。我肯定我做得很不对,因为这不管用哈。我怎样才能让这个工作?如果我试图让它通过一个函数,数据就会出错,否则我可以通过控制台将其注销。

    我得写些其他的东西来完成这篇文章……雅达雅达。

    import { takeEvery, take, call, put, all } from 'redux-saga/effects';
    import axios from 'axios';
    
    // 1. Worker Saga
    export function* asyncAjax(){
        console.log("asyncAjax")
        const url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/",
              urlSuffix = [
                "all_hour.geojson",
                "all_day.geojson",
                "all_week.geojson",
                "all_month.geojson"
              ] 
    
        for (var i = 0; i < urlSuffix.length; i++) {
    
            yield console.log("for loop index: "+i)
    
            try {
                // AJAX geoJSON
                const response = yield call(axios.get, (url+urlSuffix[i])),
                      { features } = response.data
    
                parseData(features, i)
    
    
            } catch (e) {
                alert("Data Download Error")
                console.log(e)
            }
    
        }
    
        // yield put({ type: "VIZ_INIT_SUCCESS", action: true })
    
    }
    
    function parseData(features, i){
        let arr = []
    
        // Data Parsing
        for (let feature of features) {
            let magnitude = features.properties.mag,
                location = features.properties.place,
                time = features.properties.time,
                coordinates = features.geometry.coordinates,
                quake = {
                    "magnitude": magnitude,
                    "location": location,
                    "time": time,
                    "coordinates": coordinates
                }
    
            arr.push(quake)
        }
    
        console.log(arr)
    
        // Data Sorting by Highest Magnitude
        arr.sort((a, b) => {
            return b["magnitude"] - a["magnitude"]
        })
    
        console.log("quakes saga")
        console.log(arr)
    
        // Storing in State
        put({ 
            type: "QUAKES", 
            action: { 
                "index": i,
                "value": arr  
            } 
        })
    
        // Updating Progress Text/Bar
        put({ 
            type: "PRELOADER_TEXT", 
            action: {
                "payload": `Loading Data ${i}/4`
            } 
        })
    
        put({ 
            type: "PROGRESS_COMPLETE", 
            action: {
                "payload": (i/4)*100
            } 
        })
    }
    
    
    
    // 2. Watcher Saga
    export function* watchAJAX(){
        console.log("redux-saga is executing AJAX")
        yield takeEvery('VIZ_INIT', asyncAjax)
    }
    
    // 3. Root Saga
    export default function* rootSaga(){
        yield all([
            watchAJAX(),
        ])
    };
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Abdallatif Sulaiman    6 年前

    parseData 应该是一个发电机,当你打电话的时候,你应该这样称呼它 yield call(parseData, features, i) 而且在 分析数据 功能确保使用 yield yield put({...})