【发布时间】:2018-09-21 13:20:58
【问题描述】:
我正在尝试将 async await 与 redux-thunk 中间件一起使用,但出现红屏并显示错误 Actions must be plain objects。使用自定义中间件进行异步操作。我想我没有返回正确的值类型。我按下一个按钮将 twitter 用户链接到现有的 firebase 帐户。该按钮到达一个名为 toggleTwitterAuthState 的函数:
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-native-debugger在返回异步(调度)上放置一个断点并到达那里,但是里面的代码永远不会被执行,我得到那个带有错误的红屏如上所述
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 配置是这样的,我是从 udemy 课程中学到的,那家伙使用了一个 componse 函数 https://www.udemy.com/react-native-the-practical-guide/ :
import { createStore, combineReducers, compose, applyMiddleware } from 'redux';
import thunk from "redux-thunk";
import foundationsReducer from './reducers/foundations';
import sponsorsReducer from './reducers/sponsors';
import authReducer from './reducers/auth';
const rootReducer = combineReducers({
foundations: foundationsReducer,
sponsors: sponsorsReducer,
auth:authReducer
});
let composeEnhancers = compose;
if (__DEV__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
}
const configureStore = () => {
return createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));
};
export default configureStore;
我在我的 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);
只是为了让您知道,redux thunk 正在为其他操作工作,但是这种异步等待情况在我看不到的方式上有所不同。知道可能出了什么问题吗?如果我摆脱了包装器和调度,函数本身就可以工作,它会做它必须做的事情,但是因为我需要这些调度来更新微调器,所以就出现了这个问题。谢谢!!
编辑:以下是操作:
export const authOperationBegan = () => {
return {
type: AUTH_OPERATION_BEGAN
};
}
export const authOperationFinished = () => {
return {
type: AUTH_OPERATION_FINISHED
};
}
export const authOperationFailed = (err) => {
return {
type: AUTH_OPERATION_FAILED,
error: err
};
}
我还有其他功能可以发送相同的 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));
});
}
};
};
【问题讨论】:
-
authOperation 操作是什么样的?
-
bspaka。我编辑了我的帖子。我在其末尾添加了已调度操作的规范。我还提供了一个函数示例,该函数调度了相同的 3 个动作并且工作正常......
标签: reactjs react-native redux redux-thunk