英文:
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 = () => {
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 not returned
.then((onlySmr) => {
dispatch(getDataFromGoogleAction(onlySmr)) // <-- onlySmr undefined
});
}
}
Good Code
I suggest just logging onlySmr
in the last .then
-able right before dispatching the getDataFromGoogleAction
action`.
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); // <-- log onlySmr value
dispatch(getDataFromGoogleAction(onlySmr)); // <-- onlySmr defined
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论