【问题标题】:How to fix error in redux-thunk: Expected an assignment or function call and instead saw an expression如何修复 redux-thunk 中的错误:需要赋值或函数调用,而是看到一个表达式
【发布时间】:2019-11-29 20:42:04
【问题描述】:

我正在尝试在 redux-thunk 的帮助下将图像发送到 firebase 商店。没有 redux,代码可以完美运行。

已编辑:这是错误的 thunk ./src/Thunks/index.js


    // uploadReducer Thunk

    import {uploadImagesPending, uploadImagesProgress, uploadImagesSuccess, uploadImagesError} from '../Actions';
    import fire, { auth } from '../fire';
    // import  store  from '../index';

    function uploadImages(images) {
        return dispatch => {
            dispatch(uploadImagesPending());

            // const images = store.getState().images;
            const userID = fire.auth().currentUser.uid;
            const uploadTask = fire.storage().ref(`users/${userID}`).child(`images/${images.name}`).put(images);

            uploadTask.on('state changed', (snapshot) => {
                    const progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
                    dispatch(uploadImagesProgress(progress))
            }),
            (error) => {
                dispatch(uploadImagesError(error))
            },
            () => {
                fire.storage().ref('images').child(images.name).getDownloadURL().then(url => {
                    dispatch(uploadImagesSuccess(url))
                })
            }
        }
    }

    export default uploadImages;

简单的动作和reducer...

图片来自上传组件

 handleChange(event) {
        if(event.target.files[0]) {
            const targetImages = event.target.files[0];

            this.setState({
                images: targetImages
            });
        }
    }
    uploadImage() {
        const { uploadImages } = this.props;
        const { images } = this.state;
        uploadImages(images);
    }

但抓住

编译失败。 ./src/Thunks/index.js 第 17 行:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions 搜索关键字以详细了解每个错误。

【问题讨论】:

  • 请发此文件/src/Thunks/index.js
  • 嗨!就是 /src/Thunks/index.js )

标签: firebase react-redux redux-thunk


【解决方案1】:

问题是你把)uploadTask.on 函数放在错误的位置。正确的应该是

uploadTask.on('state changed', (snapshot) => {
  const progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
  dispatch(uploadImagesProgress(progress));
},
(error) => {
  dispatch(uploadImagesError(error));
},
() => {
  fire.storage().ref('images').child(images.name).getDownloadURL()
    .then((url) => {
      dispatch(uploadImagesSuccess(url));
    });
});

【讨论】:

    猜你喜欢
    • 2015-05-22
    • 2022-01-24
    • 2021-01-09
    • 1970-01-01
    • 2020-02-28
    • 2012-02-23
    • 1970-01-01
    • 2023-03-26
    • 2023-03-17
    相关资源
    最近更新 更多