Axios在第一个数据行中获得调用结果,该结果是后续行中的值之和

我有一个问题,在我进行axios.get调用并检查返回的数据后,第一行数据以某种方式添加了后续行的值。

开发工具中来自Node.js端以及console.log responseText的数据为:

0: {id: 9994, site: "Store 1", Date: "2020-04-01", Income: 7.73}
1: {id: 9994, site: "Store 1", Date: "2020-04-02", Income: 10.01}
2: {id: 9994, site: "Store 1", Date: "2020-04-03", Income: 6.78}
3: {id: 9994, site: "Store 1", Date: "2020-04-04", Income: 6.91}
4: {id: 188, site: "Store 2", Date: "2020-04-01", Income: 10.03}
5: {id: 188, site: "Store 2", Date: "2020-04-02", Income: 6.51}
6: {id: 188, site: "Store 2", Date: "2020-04-03", Income: 7.93}
7: {id: 188, site: "Store 2", Date: "2020-04-04", Income: 7.91}

但数据数组中收到的数据是:

0: {id: 9994, site: "Store 1", Date: "2020-04-01", Income: 31.430000000000003}
1: {id: 9994, site: "Store 1", Date: "2020-04-02", Income: 10.01}
2: {id: 9994, site: "Store 1", Date: "2020-04-03", Income: 6.78}
3: {id: 9994, site: "Store 1", Date: "2020-04-04", Income: 6.91}
4: {id: 188, site: "Store 2", Date: "2020-04-01", Income: 32.379999999999995}
5: {id: 188, site: "Store 2", Date: "2020-04-02", Income: 6.51}
6: {id: 188, site: "Store 2", Date: "2020-04-03", Income: 7.93}
7: {id: 188, site: "Store 2", Date: "2020-04-04", Income: 7.91}

因此,商店的第一行(在这种情况下为0和4)已将给定期间的给定商店的所有后续行收入值相加。这是第一个console.log,在按照以下功能运行任何计算之前。因此,任何向前推进的计算都是错误的。

我的功能如下:

  axiosCall = (url, stateVariable, keyName, sumOrMedian) => {

    axios.get('/'+url)
    .then(response => {
        console.log(response);

        let axiosResponse = null;
        let arrayOfVals = [];
        if (response.data){
            axiosResponse = response.data.reduce((acc, cur) => {
                let inAcc = false;
                acc.forEach(o => {
                    if (o.id === cur.id) { // if obj store is already in new array, increase sum
                        if (sumOrMedian === 'Median'){
                            //create an array of values (to work out the medians after - exclude zeros)
                            if (cur[keyName] > 0) {
                                arrayOfVals.push(cur[keyName]); //build array of results for this store
                            }
                        }else{
                            //Sum of results instead of median of results
                            o[keyName] += cur[keyName]; //
                        }
                        inAcc = true;
                    }
                    if (sumOrMedian === 'Median'){
                        if (arrayOfVals){
                            o[keyName] = stats.median(arrayOfVals).toFixed(2); //median of generated array to 2 decimal places
                        }
                    }
                });
                if (!inAcc) {
                    acc.push(cur); // if obj store isn't already in new array, add it
                }
                return acc;
            }, []);
        }

        let merged = this.joinObjects(axiosResponse, this.state.data); //insert results into state
    })
    .catch(error => {
        console.log(error);
        this.setState({ dataError: true });
    });
  }

该功能的目的是获取该时间段内商店的总收入。它还可以评估各天商店的中位数(平均值),从而报告平均每日收入(通过为每家商店建立值数组并最终运行统计函数来确定中位数)。

但是,在进行任何计算之前,(针对每个商店)数据的初始行将对该商店的所有其他行进行汇总。我直接从Node.js调用了数据并对其进行了审核,我试图将头添加到axios.get调用中,甚至尝试将GET更改为POST。没有运气,同样的结果。

谢谢。

评论