React Reducer获取payload:undefined

huangapple go评论67阅读模式
英文:

React Reducer get payload:undefined

问题

Reducer:

const defaultState = {
  data: [],
}

const GET_DATA_FROM_GOOGLE = "GET_DATA_FROM_GOOGLE"

export const googleDataReducer = (state = defaultState, action) => {
  switch (action.type) {
    case "GET_DATA_FROM_GOOGLE":
      console.log('1 reducer')
      console.log(action) // there are payload: undefined
      console.log('2 reducer')
      return {
        ...state,
        data: action.payload
      }    

    default:
      return state
  }
}

export const getDataFromGoogleAction = (payload) => ({
  type: GET_DATA_FROM_GOOGLE,
  payload
})

Where I fetch data from Google Sheets:

import { getDataFromGoogleAction } from "../store/googleDataReducer.js";

export const fetchData = () => {
  return function(dispatch) {
    let onlySmr fetch('https://script.google.com/macros/s/AKfycbyfcEvLSna_NbO-FDPSuo_efg7tK2uVugGDGTBqpQ1mrFvNPnqEGUFbPLgD-buhj4kT/exec') 
      .then((response) => response.json())
      .then((response) => onlySmr = response['smr']) // there are normal data 
      .then((onlySmr) => console.log(onlySmr))
      .then((onlySmr) => dispatch(getDataFromGoogleAction(onlySmr))) 
  }
}
英文:

I tried to get some data from googlesheets and send it to reducer. When I get data from sheets I check it in console and when I send it to reducer and check it in console it is undefined.

Reducer:

const defaultState = {
  data: [],
}

const GET_DATA_FROM_GOOGLE = "GET_DATA_FROM_GOOGLE"

export const googleDataReducer = (state = defaultState, action) => {
  switch (action.type) {
    case "GET_DATA_FROM_GOOGLE":
      console.log('1 reducer')
      console.log(action) // there are payload:undefined 
      console.log('2 reducer')
      return {
        ...state,
        data: action.payload
      }    

    default:
      return state
  }
}

export const getDataFromGoogleAction = (payload) => ({
  type: GET_DATA_FROM_GOOGLE,
  payload
})

Where I fetch data from googlesheet:

import { getDataFromGoogleAction } from "../store/googleDataReducer.js"

export const fetchData = () => {
  return function(dispatch) {
    let onlySmr fetch('https://script.google.com/macros/s/AKfycbyfcEvLSna_NbO-FDPSuo_efg7tK2uVugGDGTBqpQ1mrFvNPnqEGUFbPLgD-buhj4kT/exec') 
      .then((response) => response.json())
      .then((response) => onlySmr = response['smr']) // there are normal data 
      .then((onlySmr) => console.log(onlySmr))
      .then((onlySmr) => dispatch(getDataFromGoogleAction(onlySmr))) 
  }
}

答案1

得分: 2

问题出在fetchData函数中。Promise链在尝试记录值的.then部分中断,特别是在你尝试记录值的地方。console.log是一个空返回。如果你想在中途记录一个值,你仍然需要在链中返回它。

不良代码

export const fetchData = () => {
  return function(dispatch) {
    let onlySmr fetch('https://script.google.com/macros/s/..../exec') 
      .then((response) => response.json())
      .then((response) => onlySmr = response['smr']) 
      .then((onlySmr) => console.log(onlySmr))     // <-- onlySmr没有返回
      .then((onlySmr) => {
        dispatch(getDataFromGoogleAction(onlySmr)) // <-- onlySmr未定义
      });
  }
}

良好代码

我建议在最后一个.then中只记录onlySmr,然后再调度getDataFromGoogleAction操作。

export const fetchData = () => dispatch => {
  fetch('https://script.google.com/macros/s/..../exec') 
    .then((response) => response.json())
    .then((data) => {
      const onlySmr = data['smr'];
      console.log(onlySmr);                       // <-- 记录onlySmr的值
      dispatch(getDataFromGoogleAction(onlySmr)); // <-- onlySmr已定义
    });
}
英文:

The issue is in the fetchData function. The Promise chain breaks returning a defined response value, specifically in the .then-able where you try to log the value. console.log is a void return. If you want to log a value mid-stream then you still need to return it in the chain.

Bad Code

export const fetchData = () =&gt; {
  return function(dispatch) {
    let onlySmr fetch(&#39;https://script.google.com/macros/s/..../exec&#39;) 
      .then((response) =&gt; response.json())
      .then((response) =&gt; onlySmr = response[&#39;smr&#39;]) 
      .then((onlySmr) =&gt; console.log(onlySmr))     // &lt;-- onlySmr not returned
      .then((onlySmr) =&gt; {
        dispatch(getDataFromGoogleAction(onlySmr)) // &lt;-- onlySmr undefined
      });
  }
}

Good Code

I suggest just logging onlySmr in the last .then-able right before dispatching the getDataFromGoogleAction action`.

export const fetchData = () =&gt; dispatch =&gt; {
  fetch(&#39;https://script.google.com/macros/s/..../exec&#39;) 
    .then((response) =&gt; response.json())
    .then((data) =&gt; {
      const onlySmr = data[&#39;smr&#39;];
      console.log(onlySmr);                       // &lt;-- log onlySmr value
      dispatch(getDataFromGoogleAction(onlySmr)); // &lt;-- onlySmr defined
    });
}

huangapple
  • 本文由 发表于 2023年3月1日 14:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定