Redux Saga Как разобрать данные - PullRequest
0 голосов
/ 29 марта 2019

Здравствуйте, первый раз, когда работаете с генераторами или саксофонами. Мне нужно проанализировать эти данные. Я уверен, что делаю это совершенно неправильно, так как это не работает, ха Как мне заставить это работать? Ошибки данных выходят, если я пытаюсь передать их через функцию, в противном случае я могу выйти из консоли.

Мне нужно написать еще кое-что, чтобы пропустить этот пост ..... Яда Яда.

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(),
    ])
};

1 Ответ

0 голосов
/ 07 апреля 2019

parseData должен быть генератором, и когда вы звоните, вы должны называть его как yield call(parseData, features, i), а в функции parseData обязательно используйте yield yield put({...})

...