使用React-Base64在图像上传过程中发生后验证失败的错误失败

发布于 2025-01-18 23:15:26 字数 1195 浏览 3 评论 0原文

我正在上传图像并将其保存到mongodb,但它显示以下错误:

消息:“后验证验证失败:selectedfile:cast

名称:“验证”

_message:“邮政验证失败”

我正在前端使用React-Base64来上传图像。

<FileBase type="file" multiple={false} onDone={(base64) => setPostData({ ...postData, selectedFile: base64})} />

我通过获取将其添加到DB:

let handleSubmit = (event) => {
        event.preventDefault();
        console.log(postData);
        fetch(`http://localhost:5000/posts/create`, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json',
                // 'Accept': 'application/json'
            },
            body: JSON.stringify(postData),
        })
        .then(response => response.json())
        .then((dataFromServer) => {
            console.info(dataFromServer);
        })
        .catch(error => console.error(error));

    }

谁能告诉我我在做什么错,我该如何解决这个问题

I am uploading an image and saving it to mongodb but it is showing following error:

message: "postMessage validation failed: selectedFile: Cast

name: "ValidationError"

_message: "postMessage validation failed"

I am using react-base64 on the front end side to upload the image.

<FileBase type="file" multiple={false} onDone={(base64) => setPostData({ ...postData, selectedFile: base64})} />

And i am adding it to DB via fetch :

let handleSubmit = (event) => {
        event.preventDefault();
        console.log(postData);
        fetch(`http://localhost:5000/posts/create`, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json',
                // 'Accept': 'application/json'
            },
            body: JSON.stringify(postData),
        })
        .then(response => response.json())
        .then((dataFromServer) => {
            console.info(dataFromServer);
        })
        .catch(error => console.error(error));

    }

Can anyone tell me what I am doing wrong and how can i fix this issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2025-01-25 23:15:26

在文件库组件中,您需要以这种方式进行。

您需要在ondone上破坏参数。这样,ondone = {({base64})=&gt; yourFunction()}

You can use this one.

<FileBase type="file" multiple={false} onDone={({base64}) => setPostData({ ...postData, selectedFile: base64})} />

In the FileBase component, you need to do in this way.

You need to destructure the parameter on onDone. In this way onDone={({base64}) => yourFunction()}

You can use this one.

<FileBase type="file" multiple={false} onDone={({base64}) => setPostData({ ...postData, selectedFile: base64})} />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文