【问题标题】:How to dispatch from actioncreator to reducer wihout error in one time?如何一次无错误地从动作创建者调度到减速器?
【发布时间】:2019-06-08 20:15:04
【问题描述】:

我正在向 Firebase 发送图像。一切都成功了,我可以将图像上传到 firebase 存储。但是在我成功之后,我的代码突然又发出了一次错误。顺便说一下,我记录了一些数字以查看发生了什么,我在控制台上得到了“1,2,3,4 then success and then failed error” .如何在不出错的情况下上传图片?我可以上传,但我现在也收到错误..提前谢谢你.. 错误消息说:“传播不可迭代实例的尝试无效”。

export const uploadPhoto = (uri, contentType = 'image/jpeg') => {
return async dispatch => {
    try {
        console.log('1');

        const userId = firebase.auth().currentUser.uid;
        const photoId = await uniqueIdGenerator();
        dispatch({ type: UPLOAD_START });
        console.log('2');

        const snapshot = await firebase.storage().ref()
            .child(`/photos/${userId}`)
            .child(photoId)
            .put(uri, { contentType });
        console.log('3');

        await firebase.firestore()
            .collection('users').doc(userId)
            .collection('photos').doc(photoId)
            .set({ url: snapshot.downloadURL });
        console.log(snapshot.downloadURL);
        console.log('4');

        await dispatch({ type: UPLOAD_SUCCESS, payload: snapshot.downloadURL });
        console.log('5');

        Actions.pop();
        console.log('6');

    }
    catch (error) {
        dispatch({ type: UPLOAD_FAILED });
        console.log(error.message);
    }
}
}

这是我的减速器:

const INITIAL_STATE = {
    data: [],
    loading: false
}

export default (state = INITIAL_STATE, { type, payload }) => {
    console.log('reducerState', state);
console.log('type,payload', type, payload);

switch (type) {
    case UPLOAD_START:
        return { ...state, data: payload, loading: true }
    case UPLOAD_SUCCESS:
        console.log('succesa girdi');
        return { ...state, data: [...state.data, payload], loading: false }
    case UPLOAD_FAILED:
        return { ...state, data: payload, loading: false }
    case GET_PHOTOS_START:
        return { ...state, data: payload, loading: true }
    case GET_PHOTOS_SUCCESS:
        return { ...state, data: payload, loading: false }
    case GET_PHOTOS_FAILED:
        return { ...state, data: payload, loading: false }
    default:
        return state;
}
}

编辑

我通过从我的 Firebase 存储中删除所有虚拟数据来解决问题。我使用模拟器来测试我的应用程序,并且一次又一次地使用相同的照片。很可能相同的照片会导致问题,因为我没有更改我的应用程序中的任何其他内容,但现在运行良好。

【问题讨论】:

  • 确切的错误信息是什么
  • 对不起,我忘了添加它。消息说:“传播不可迭代实例的尝试无效”
  • 你的 UPLOAD_SUCCESS 在做什么,能发一下吗
  • 嘿,我刚刚添加了减速器。顺便说一句,我使用 UPLOAD_SUCCESS 将我的照片网址发布到减速器。

标签: javascript reactjs firebase react-native redux


【解决方案1】:

尝试更换减速器

case UPLOAD_SUCCESS:
    console.log('succesa girdi');
    return { ...state, data: [...state.data, payload], loading: false }

case UPLOAD_SUCCESS:
    console.log('succesa girdi');
    return { ...state, data: payload, loading: false }

我认为该错误试图警告您。我不确定您为什么要尝试两次传播状态

更新

在这种情况下试试这个

case UPLOAD_SUCCESS:
    console.log('succesa girdi');
    let urls= state.data
    urls.push(payload)
    return { ...state, data: urls, loading: false }

【讨论】:

  • 您好,首先感谢您的回答。我需要状态中的所有 url,因此我尝试返回所有先前处于状态的 url 和来自数组中的有效负载的新 url。通过这种方式,我可以在另一个页面中显示所有 url。如果我像你说的那样做,我只会得到一个我刚刚添加的网址,但我不能接受另一个网址。
  • 请尝试更新后的答案。我不确定这是否正确,如果可行,请告诉我
  • 我尝试了您的新解决方案。首先,我在控制台上看到了成功,然后又突然 catch 块在动作中起作用,然后在减速器中的 upload_failed 案例起作用。顺便说一句,我这次看到了不同的错误:无法读取未定义的属性“推送”
  • 嘿,我解决了这个问题。我从 Firebase 存储中删除了所有虚拟数据。我试图用相同的照片测试我的应用程序。很可能同一张照片会导致问题,因为我没有更改任何其他内容。但现在一切都运行良好。顺便说一句,非常感谢你的努力。我将使用此解决方案更新我的问题。
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2016-08-12
  • 1970-01-01
相关资源
最近更新 更多