开发者问题收集

TypeError:当我尝试获取第二次时,未定义不是对象(评估“ e.type”)

2020-08-31
4799

我不明白为什么每次我创建或订阅帖子时,我都无法呈现任何获取函数,并且出现 TypeError:undefined 不是对象(评估“e.type”)。所以这里有一些代码。

我有一个 rails API 和一个 react native 在其中获取数据。当我使用 Sub 或创建 rails 时,会向我发送状态 200 消息,所以它可以工作。但如果我返回我的 feedlist 并选择任何帖子或尝试注销,我会收到此错误:TypeError:undefined 不是对象(评估“e.type”)

sub

export function onSub(data){
        return dispatch => {
            dispatch(fetchPostsPending());//to change?
            return fetch('localhost/api/v0/posts/'+data.id+'/records',{
            method:'POST',
            headers:{
                "access-token": data.accessToken,
                "token-type":   data.tokenType,
                "client":       data.client,
                "expiry":       data.expiry,
                "uid":          data.uid
                }
            })
            .then((response)=>{
                if(response.ok){
                    if(response.headers.map['access-token'] == ""){
                        console.log("Response Token:",response.headers.map);
                    }
                    else{
                        console.log("New Token")
                        console.log("Response Token:",response.headers.map);
                        dispatch(updateToken(response.headers.map));
                    }
                    dispatch(fetchPostUpdate())
                }
                else{
                    return response.json()
                    .then((response)=>{
                        console.log("Response:",response);
                        dispatch(fetchPostsError);
                    })
                }
            })
            .catch((error)=>{
                console.log("error:",error);
                dispatch(fetchPostsError);
            })
        }
    }

创建帖子

export function onCreate(data){
        return dispatch => {
            dispatch(fetchPostsPending());
            return fetch('localhost/api/v0/posts',{
                method:'POST',
                headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                "access-token": data.accessToken,
                "token-type":   data.tokenType,
                "client":       data.client,
                "expiry":       data.expiry,
                "uid":          data.uid
                },
                body: JSON.stringify({
                    "title":data.title,
                    "content":data.content,
                    "created_by":data.created_by,
                    "entry":data.entry,
                    "category_id":data.category_id,
                    "rdv":data.rdv,
                    "tag1":data.tag1,
                    "tag2":data.tag2,
                    "tag3":data.tag3,
                    "user_id":data.user_id
                })
            })
                .then((response) => {
                    if(response.ok){
                        if(response.headers.map['access-token'] == ""){
                            console.log("Response Token:",response.headers.map);
                        }
                        else{
                            console.log("New Token")
                            console.log("Response Token:",response.headers.map);
                            dispatch(updateToken(response.headers.map));
                        }
                    
                        response.json().then((responseJSON) => {
                            console.log("responseJSON :", JSON.stringify(responseJSON));
                            Alert.alert('New Post', 'Le Post a Bien ete créer');
                            dispatch(fetchPostCreate());
                            dispatch(fetchPostsPending());
                        })
                    }
                    else{
                        response.json().then((responseJSON) => {
                            console.log("responseJSON",JSON.stringify(responseJSON));
                            Alert.alert('Post ', 'something wrong');
                            dispatch(fetchPostsPending());
                        })
                    }

                })
                .catch((error) => {
                    console.log("error",error);
                    Alert.alert('Post', 'Some error occured, please retry');
                    dispatch(fetchPostsError(error));
                });
            }
        }

Postreducer

import {FETCH_POST_PENDING, FETCH_POST_SUCCESS,FETCH_ONE_POST_SUCCESS, FETCH_POST_ERROR,
    FETCH_POST_CREATE,FETCH_POST_UPDATE,FETCH_POST_DELETE} from '../actions/postActions';

const INITIAL_STATE = {
    pending: false,
    posts: [],
    onepost:[],
    create: false,
    update:false,
    delete:false,
    error: null
}

export default function post(state = INITIAL_STATE, action) {
    switch(action.type) {
        case FETCH_POST_PENDING: 
            return {
                ...state,
                pending: !state.pending
            }
        case FETCH_POST_SUCCESS:
            return {
            ...state,
            pending: false,
            posts:action.payload
            }
        case FETCH_ONE_POST_SUCCESS:
            return {
                ...state,
                pending: false,
                onepost:action.payload
            }
        case FETCH_POST_CREATE:
            return {
                ...state,
                create: !state.create
            }
        case FETCH_POST_UPDATE:
            return {
                    ...state,
                    update: !state.update
                }
        case FETCH_POST_DELETE:
            return {
                ...state,
                delete: !state.delete
            }
        case FETCH_POST_ERROR:
            return {
                ...state,
                pending: false,
                error:action.error
            }

        default: 
            return state;
    }
}

postAction

// Types
export const FETCH_POST_PENDING = 'FETCH_POST_PENDING';
export const FETCH_POST_SUCCESS = 'FETCH_POST_SUCCESS';
export const FETCH_ONE_POST_SUCCESS = 'FETCH_ONE_POST_SUCCESS';
export const FETCH_POST_ERROR = 'FETCH_POST_ERROR';
export const FETCH_POST_CREATE = 'FETCH_POST_CREATE';
export const FETCH_POST_UPDATE = 'FETCH_POST_UPDATE';
export const FETCH_POST_DELETE = 'FETCH_POST_DELETE';


//Actions

export function fetchPostsPending() {
    return {
        type: FETCH_POST_PENDING
    }
}
//Index GetPosts
export function fetchPostsSuccess(posts) {
    return {
        type: FETCH_POST_SUCCESS,
        payload: posts
    }
};
//Show
export function fetchOnePostSuccess(onepost) {
  return {
      type: FETCH_ONE_POST_SUCCESS,
      payload: onepost
  }
};
export function fetchPostCreate(){
  type: FETCH_POST_CREATE
};
export function fetchPostUpdate(){
  type: FETCH_POST_UPDATE
};
export function fetchPostDelete(){
  type: FETCH_POST_DELETE
};
// manque ds ls Action Create Updates Deletes**

export function fetchPostsError(error){
    return {
        type: FETCH_POST_ERROR,
        error: error
    }
}

谢谢大家

2个回答

您是否在使用“redux-thunk”? 如果没有,要使用“异步操作创建器”,您需要应用中间件“redux-thunk”。

Daniel Pantalena
2020-08-31

好的,我忘记了 redux 操作中的 return {} :fetchPostCreate, fetchPostUpdates, fetchPostDelete。

sassani134
2020-09-01