Reactjs 与Redux thunk异步/等待。获取错误:操作必须是普通对象。使用自定义中间件进行异步操作
我正在尝试将async await与redux thunk中间件一起使用,但我得到了红屏,错误操作必须是普通对象。使用自定义中间件进行异步操作。我想我没有返回正确类型的值。我按下按钮将twitter用户链接到现有的firebase帐户。该按钮到达一个名为toggleTwitterAuthState的函数:Reactjs 与Redux thunk异步/等待。获取错误:操作必须是普通对象。使用自定义中间件进行异步操作,reactjs,react-native,redux,redux-thunk,Reactjs,React Native,Redux,Redux Thunk,我正在尝试将async await与redux thunk中间件一起使用,但我得到了红屏,错误操作必须是普通对象。使用自定义中间件进行异步操作。我想我没有返回正确类型的值。我按下按钮将twitter用户链接到现有的firebase帐户。该按钮到达一个名为toggleTwitterAuthState的函数: export const toggleTwitterAuthState = (twitterIsCurrentlyLinked, contributorUserId) => {
export const toggleTwitterAuthState = (twitterIsCurrentlyLinked, contributorUserId) => {
let actionName = "";
if (twitterIsCurrentlyLinked) {
console.log("Unlinking twitter");
actionName = "TWITTER_UNLINK";
unlinkTwitterAccount(contributorUserId);
} else {
console.log("Linking twitter");
linkTwitterAccount(contributorUserId);
actionName = "TWITTER_LINK";
}
};
它调用函数linkTwitterAccount,我正在使用react本机调试器在返回异步调度上放置一个断点,它到达了那里,但是里面的代码从未执行过,我得到了带有上述错误的红色屏幕
linkTwitterAccount = (contributorUserId) => {
return async (dispatch)=>{
console.log("about to link twitter user");
RNTwitterSignIn.init(config.twitter.consumer_key, config.twitter.consumer_secret);
dispatch(authOperationBegan());
let linkToTwitterResult;
let twitterTokensObject;
let loginData;
//get credentials
try {
loginData = await RNTwitterSignIn.logIn();
console.log("Twitter login data", loginData);
} catch (err) {
console.log("Error with twitter login result", error);
dispatch(authOperationFailed(err));
}
//link to react native firebase
try {
const {
authToken,
authTokenSecret
} = loginData;
const user = firebase.auth().currentUser;
// create a new firebase credential with the token
const twitterCredential = firebase.auth.TwitterAuthProvider.credential(authToken, authTokenSecret);
console.log(twitterCredential);
// link to this account with credential
const linkingResult = await user.linkAndRetrieveDataWithCredential(twitterCredential);
console.log("Success Linking twitter", linkingResult);
var currentUser = linkingResult.user;
var displayName;
var photoUrl;
var email;
var phoneNumber;
var twitterUserId;
currentUser.providerData.map(elem => {
if (elem.providerId == "twitter.com") {
displayName = elem.displayName;
photoUrl = elem.photoURL;
email = elem.email;
phoneNumber = elem.phoneNumber;
twitterUserId = elem.uid;
}
});
twitterTokensObject = {
"contributor_user_id": contributorUserId,
"twitter_id": twitterUserId,
"twitter_access_token": authToken,
"twitter_access_token_secret": authTokenSecret,
"display_name": displayName,
"photo_url": photoUrl,
"phone_number": phoneNumber,
"email": email
};
} catch (err) {
alert("Error linking asociando cuenta: " + err);
dispatch(authOperationFailed(err));
}
//TODO: upsert twitter user data to DB
dispatch(authOperationFinished());
}
}
我的redux thunk配置是这样的,我从我的一门课程中学到的,在这门课程中,一个人使用了一个componse函数:
我在index.js上使用此redux thunk配置:
import React from 'react';
import { AppRegistry } from 'react-native';
import { Provider } from 'react-redux';
import App from './App';
import configureStore from './src/store/configureStore';
const store = configureStore();
const RNRedux = () => (
<Provider store={store}>
<App />
</Provider>
);
AppRegistry.registerComponent('rnfundkers', () => RNRedux);
我还有其他功能可以分派相同的3个动作,它们工作得很好,例如:
export const tryAuth = (authData, authMode) => {
return dispatch => {
dispatch(authOperationBegan());
const email = authData.email,
password = authData.password;
if (authMode === "signup") {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((user) => {
// TODO: upsert user to our db
dispatch(authOperationFinished());
})
.catch((error) => {
const {
code,
message
} = error;
dispatch(authOperationFailed(err));
});
} else if (authMode == "login") {
firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password)
.then((data) => {
dispatch(authOperationFinished());
})
.catch((error) => {
const {
code,
message
} = error;
console.log("error", message);
dispatch(authOperationFailed(err));
});
}
};
};
你必须发出你的声音
export const toggleTwitterAuthState = (twitterIsCurrentlyLinked, contributorUserId) => dispatch => {
let actionName = "";
if (twitterIsCurrentlyLinked) {
console.log("Unlinking twitter");
actionName = "TWITTER_UNLINK";
unlinkTwitterAccount(contributorUserId);
} else {
console.log("Linking twitter");
dispatch(linkTwitterAccount(contributorUserId));
actionName = "TWITTER_LINK";
}
};
authOperation操作看起来像什么?bspaka。我编辑了我的帖子。我在末尾添加了已调度操作的规范。我还包括了一个函数的例子,它可以分派相同的3个动作,并且可以正常工作……谢谢。它创造了奇迹。完整的代码将是您的解决方案,再加上用返回调度包装所有内容,这样该功能就可用了。是的,您是对的,在匆忙回答时,答案不完整,我添加了更多细节。
export const tryAuth = (authData, authMode) => {
return dispatch => {
dispatch(authOperationBegan());
const email = authData.email,
password = authData.password;
if (authMode === "signup") {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((user) => {
// TODO: upsert user to our db
dispatch(authOperationFinished());
})
.catch((error) => {
const {
code,
message
} = error;
dispatch(authOperationFailed(err));
});
} else if (authMode == "login") {
firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password)
.then((data) => {
dispatch(authOperationFinished());
})
.catch((error) => {
const {
code,
message
} = error;
console.log("error", message);
dispatch(authOperationFailed(err));
});
}
};
};
export const toggleTwitterAuthState = (twitterIsCurrentlyLinked, contributorUserId) => dispatch => {
let actionName = "";
if (twitterIsCurrentlyLinked) {
console.log("Unlinking twitter");
actionName = "TWITTER_UNLINK";
unlinkTwitterAccount(contributorUserId);
} else {
console.log("Linking twitter");
dispatch(linkTwitterAccount(contributorUserId));
actionName = "TWITTER_LINK";
}
};