2017-01-15 37 views
2

我正在從RecommendationItemActions.js操作文件中的所有操作接收到Actions may not have an undefined "type" property. Have you misspelled a constant?錯誤。操作可能沒有未定義的「type」屬性 - React-Native/Redux

enter image description here

出現此問題與包含在這兩個出口。

import firebase from 'firebase'; 
import {} from 'redux-thunk'; 

import { 
    RECOMMENDATION_ITEM_UPDATE, 
    RECOMMENDATION_ITEMS_FETCH_SUCCESS 
} from './types'; 

export const recommendationItemUpdate = ({ prop, value }) => { 
    //ex. Want to change name? Send in prop [name] and value [ what you want the new name to be ] 
    return { 
    type: RECOMMENDATION_ITEM_UPDATE, 
    payload: { prop, value } 
    }; 
}; 


export const recommendationItemsFetch =() => { 
    const { currentUser } = firebase.auth(); 

    return (dispatch) => { 
    firebase.database().ref(`/users/${currentUser.uid}/recommendationItems`) 
    //snapshot is the value found 
    //snapshot is an object that describes what data is there 
     .on('value', snapshot => { 
     console.log(snapshot.val()); 
     dispatch({ 
      type: RECOMMENDATION_ITEMS_FETCH_SUCCESS, 
      payload: snapshot.val() 
     }); 
     //snapshot.val() is the actual data at snapshot 
     }); 
    }; 
}; 

這些types的進口type.js文件

export const RECOMMENDATION_ITEMS_FETCH_SUCCESS = 'recommendation_items_fetch_success'; 
export const RECOMMENDATION_ITEM_UPDATE = 'recommendation_item_update'; 

的行動是在actions文件夾

export * from './RecommendationItemActions'; 

他們的RecommendationItemCreate.js文件中導入通過index.js文件導出中明確規定

import React, { Component } from 'react'; 
import { connect } from 'react-redux'; 
import { Text, Image, View } from 'react-native'; 
import { recommendationItemUpdate, recommendationItemCreate } from '../../actions'; 
import { CardSection, Input, Button } from './../common'; 
import GooglePlacesAutocomplete from './../googlePlaces/GooglePlacesAutocomplete'; 

而且我Input組件

<Input 
    label="ADDRESS" 
    placeholder="Address" 
    value={this.props.formattedAddress} 
    onChangeText={ 
     value => this.props.recommendationItemUpdate({ prop: 'formattedAddress', value }) 
    } 
    /> 

我找不到這將導致Actions may not have an undefined "type" property,現在已經被敲我的頭這幾天任何錯誤的onChangeText內引用。明確定義了type,並且可以在整個使用過程中準確追蹤。

最接近的參考實測值 React+Redux - Uncaught Error: Actions may not have an undefined 「type」 property

這仍然是無益的。這裏是我的createStore作爲參考:

import React, { Component } from 'react'; 
import { Provider } from 'react-redux'; 
import { createStore, applyMiddleware } from 'redux'; 
import firebase from 'firebase'; 
import ReduxThunk from 'redux-thunk'; 
import reducers from './reducers'; 
import Router from './Router'; 

class App extends Component { 
    componentWillMount() { 
    const config = { 
     REMOVED FOR EXAMPLE 
    }; 
    firebase.initializeApp(config); 
    } 

    render() { 
    const store = createStore(reducers, {}, applyMiddleware(ReduxThunk)); 
    //applyMiddleware is a store-enhancer - it adds additional functionality 
    //to store 

    return (
     <Provider store={store}> 
     <Router /> 
     </Provider> 
    ); 
    } 
} 

export default App; 

回答

1

我相信,我以前打這樣的一個問題,它是由一個thunk,而不是行動的創建者內派遣一個普通的對象,比如你在做什麼引起的。你可以嘗試創建一個recommendationItemsFetchSuccess動作創建器並調度它嗎?如:

const recommendationItemsFetchSuccess = (snapshotVal) => { 
 
    return { 
 
    type: RECOMMENDATION_ITEMS_FETCH_SUCCESS, 
 
    payload: snapshotVal 
 
    }; 
 
} 
 

 
export const recommendationItemsFetch =() => { 
 
    const { currentUser } = firebase.auth(); 
 

 
    return (dispatch) => { 
 
    firebase.database().ref(`/users/${currentUser.uid}/recommendationItems`) 
 
     .on('value', snapshot => { 
 
     dispatch(recommendationItemsFetchSuccess(snapshot.val())); 
 
     }); 
 
    }; 
 
};

+2

嘿尼克,感謝您的回答。這無法解決我遇到的問題,但我很欣賞你的迴應。實際問題最終成爲我的減速器中的一個不正確的參考。我在RecommendationItemsList.js文件中導出了具有與我的參考相對比的不正確鍵的RecommendationItemReducer。 – DJSrA

相關問題