Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用react js将图像从前端上载到Mongo db时,控制台中出现其他端口错误_Node.js_Reactjs_Mongodb_Express - Fatal编程技术网

Node.js 使用react js将图像从前端上载到Mongo db时,控制台中出现其他端口错误

Node.js 使用react js将图像从前端上载到Mongo db时,控制台中出现其他端口错误,node.js,reactjs,mongodb,express,Node.js,Reactjs,Mongodb,Express,我正在尝试将图像从前端上传到mongodb数据库。邮递员一切正常,但每当我尝试从前端上传图像时,控制台日志中就会出现错误的端口错误。(我正试图将映像上载到端口号3002,但在端口显示为9000时出现API路由错误。我的前端在端口9000上运行,后端在3002上运行 这是我用来上传图像的文件 import { Avatar, Input } from '@material-ui/core' import { SettingsInputAntenna } from '@material-ui/ico

我正在尝试将图像从前端上传到mongodb数据库。邮递员一切正常,但每当我尝试从前端上传图像时,控制台日志中就会出现错误的端口错误。(我正试图将映像上载到端口号3002,但在端口显示为9000时出现API路由错误。我的前端在端口9000上运行,后端在3002上运行

这是我用来上传图像的文件

import { Avatar, Input } from '@material-ui/core'
import { SettingsInputAntenna } from '@material-ui/icons'
import axios from 'axios'
import React, { useState } from 'react'
import { useStateValue } from '../StateProvider'
import './MessageSender.css' 
import FormData from 'form-data'


const MessageSender = () => {
    const [input,setInput]=useState('')
    const [imageUrl,setImageUrl]=useState('')
    const [image,setImage]=useState(null)
    const [{user},dispatch]=useStateValue()

    const handleChange=(e)=>{
        if(e.target.files[0]){
            setImage(e.target.files[0]);
        }
    }

    const handleSubmit=async(e)=>{

        e.preventDefault()

        if(image){
            const imgForm=new FormData()
            imgForm.append('file',image,image.name)


            axios.post('/upload/image',imgForm,{
                headers:{
                    'accept':'application/json',
                    'Accept-language':'en-US,en;q=0.8',
                    'Content-Type':`multipart/form-data; boundary=${imgForm._boundary}`,
                }
            }).then((res)=>{
                
                console.log(res.data)

                const postData={
                    text: input,
                    imgName: res.data.filename,
                    user:user.displayName,
                    avatar:user.photoURL,
                    timestamp: Date.now()
                }

                console.log(postData)
                savePost(postData)
            }).catch(err=>{

                console.log(err);

            })
        }

        else{
            const postData ={
                text: input,
                user: user.displayName,
                avatar : user.photoURL,
                timestamp: Date.now()
            }

            console.log(postData)
            savePost(postData)
        }

        setImageUrl('')
        setInput('')
        setImage(null)

    }


    const savePost=async(postData)=>{
        await axios.post('/upload/post',postData)
        .then((res)=>{
            console.log(res)
        })
    }
    return (
        <div className='messageSender'>
            <div className='messageSender_top'>
                <div className="avatar_post_class">
                <Avatar src={user.photoURL}/>
                </div>
                
                <form>
                    <div className="post_text">
                    <input type="text" className='messegeSender_input' placeholder="Whats`s up with you ?" value={input} onChange = {(e)=>setInput(e.target.value)}/>
                    </div>
                   
                    <div className="file_selection">
                    <input type="file" id="fileselectionid" className='messeageSender_fileSelector' onChange= {handleChange}/>
                    </div>
                    <div className="post_button">
                        <button onClick={handleSubmit} type='Submit' id="postbutton"> Post </button>
                    </div>
                    
                </form>
            </div>
            
        </div>
    )
}

export default MessageSender


您没有上载到端口3002,因为您正在导入
axios
而不是
/axios.js

import axios from 'axios'
应该是

import axios from '<path to axios.js>/axios'
从“/axios”导入axios
import axios from '<path to axios.js>/axios'