gpt4 book ai didi

javascript - Redux Thunk函数后的回调: Uncaught TypeError: Cannot read property 'then' of undefined

转载 作者:行者123 更新时间:2023-12-01 03:11:50 24 4
gpt4 key购买 nike

问题

我在 redux 中有一个异步函数(带有 redux thunk),它在 redux 存储中设置一个值。它在我的应用程序中多次使用,但是,我希望在使用 redux 函数设置的新值运行 redux 函数后发生不同的事情。似乎是回调的一个很好的理由,对吧?所以我尝试这样做,但出现以下错误:

Uncaught TypeError: Cannot read property 'then' of undefined

尝试修复逻辑

认为原因可能是虽然 redux 函数调度操作,但它实际上并没有返回任何内容。因此,我在 redux 函数中添加了 returns ,认为该函数会将某些内容返回给调用它的组件,这将解决我收到的错误。运气不好。

<小时/>

问题:

有谁知道在异步 redux 函数(使用 redux thunk)运行并完成在 redux 存储中设置新值之后如何执行函数?

<小时/>

我最近尝试解决问题

组件

import {fetchUser} from './../actions/index.js';

class MyComponent extends React.Component {

constructor(props) {
super(props);
}

componentDidMount() {
// Attempt #1: this.props.fetchUserData()); // originally I attempted just running one function and then the rest without a callback but the new value was not set in redux store before the next function that needed it ran
// Attempt #2: this.props.fetchUserData().then(function() => { // I also tried running just a standard .then(function()
// Attempt #3: (line below)
this.props.fetchUserData().then((resultFromReduxFunction) => {
console.log("fetchUserData result = " + resultFromReduxFunction);

// do some stuff with the new data
console.log("this.props.reduxData.userHairColor = " + this.props.reduxData.userHairColor);
console.log("this.props.reduxData.userHeight = " + this.props.reduxData.userHeight);
});
}
}

function mapStateToProps(state) {
return {
reduxData: state.user
};
}

function matchDispatchToProps(dispatch) {
return bindActionCreators({
fetchUserData: fetchUserData
}, dispatch)
}

export default connect(mapStateToProps, matchDispatchToProps)(MyComponent);

Action 创建器

export const set = (idToSet, payloadToSet) => {
return {
type: 'SET',
id: idToSet,
payload: payloadToSet
}
}

export const fetchUserData = (callbackFunction) => {
console.log("fetchUserData triggered...");
return (dispatch, getState) => {
firebase.auth().onAuthStateChanged(function (user) {
if (!user) {
console.log("no user logged in");
return false // Added in attempt to fix callback
} else {
// fetch some more user data
firebase.database().ref().child('users').child(user.uid).on('value', function(snapshot) {
var userHairColor = snapshot.child(userHairColor).val();
var userHeight = snapshot.child(userHeight).val();
dispatch(set('userHairColor', userHairColor));
dispatch(set('userHeight', userHeight));
return true // Added in attempt to fix callback
})
}
})
}
}

Redux 商店

const initialState = {
userHairColor: "",
userHeight: ""
}

export default function (state=initialState, action) {
switch(action.type) {
case 'SET':
return {
...state,
[action.id]: action.payload
}
default:
return {
...state
}
}
}

提前感谢您的帮助

最佳答案

要使用.then(...),你的thunk必须返回一个Promise。 Firebase 的 onAuthStateChanged 似乎返回一个取消订阅函数,而不是 Promise,因此即使您返回它,它也不允许您在操作创建者调用站点链接其他回调。并且您无法从传递给 onAuthStateChanged 的回调中返回,因为此时您处于不同的调用堆栈中(它是异步的)。

您要做的是将回调函数传递给您的操作创建者,它需要从数据获取的成功回调中调用该函数。

类似这样的事情:

export const fetchUserData = (callbackFunction) => {
return (dispatch, getState) => {
firebase.auth().onAuthStateChanged(function (user) {
if (!user) {
console.log("no user logged in");
// You can pass false to your callback here, so that it knows the auth was unsuccessful
callbackFunction(false);
} else {
// fetch some more user data
firebase.database().ref().child('users').child(user.uid).on('value', function(snapshot) {
var userHairColor = snapshot.child(userHairColor).val();
var userHeight = snapshot.child(userHeight).val();
dispatch(set('userHairColor', userHairColor));
dispatch(set('userHeight', userHeight));
// Passing true so your callback knows it was a success
callbackFunction(true);
})
}
})
}
}

然后根据需要使用它:

this.props.fetchUserData((success) => {
console.log("fetchUserData result = " + success);

// do some stuff with the new data
console.log("this.props.reduxData.userHairColor = " + this.props.reduxData.userHairColor);
console.log("this.props.reduxData.userHeight = " + this.props.reduxData.userHeight);
});

这里发生了一些回调 hell ,但为了避免这种情况,您必须使用 Firebase API 的“ promise ”版本,在这种情况下,您还可以从 thunk 返回 promise ,以便任何使用您的操作创建者可以附加一个.then(...)

关于javascript - Redux Thunk函数后的回调: Uncaught TypeError: Cannot read property 'then' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com