1

i am trying to upload a bunch of images from the front-end (React) website into the back-end(Express, Mongoose) localhost. However, It seems the data is sent, but the server does not received it.

If i remove the fileList row from the database, the rest of the product attribute can be seen in the localhost. enter image description here enter image description here

However if i add the fileList row into the database type, i cannot see any data in the localhost. enter image description here

It seems the data is sent, but the server does not received it. Therefore, i think there is something wrong with my database type definition or back-end code. Can anyone help me? Thank you so much!

Here is my Database type

product:{
        'productName':{type:String, 'require':true},
        'productCategory':{type:[String], 'require':true},
        'productSubcategory':{type:String, 'require':true},
        'productTag':{type:[String], 'require':true},
        'productPrice':{type:String, 'require':true},
        'productDescription':{type:String, 'require':true},
        'fileList':{type:[Buffer], 'require':true}
    }

Here is my back-end code

Router.post('/addProduct', function(req, res){  
    const {type} = req.body 
    const {productName} = req.body
    const body = req.body
    Product.findOne({productName},function(err,doc){

        const productModel = new Product(req.body)
        productModel.save(function(e,d){
            if (e) {
                return res.json({code:1, msg:'Something goes wrong'})
            }       
            return res.json({code:0, data:req.body})
        })              
    })
})

Here is my related front end code

        this.state = {
            tagData :[],
            categoryDate:[],
            productName:'',
            productCategory:'',
            productSubcategory:'',
            productTag:'',
            productPrice:'',
            productDescription:'',
            previewVisible: false,
            previewImage: '',
            fileList: [],
            uploading: false,
        }

    handleChange = ({ fileList }) => this.setState({ fileList });

    handleSubmit = e => {
      e.preventDefault();
      this.props.form.validateFields((err, values) => {
        if (!err) {
          this.setState({            

            productName:values.ProductName,
            productCategory:values.ProductCategory,
            productSubcategory:values.ProductSubcategory,
            productTag:values.ProductTag,
            productPrice:values.ProductPrice,
            productDescription:values.ProductDescription,

          },function(){
            this.props.addProduct(this.state)
            console.log('Received values of form: ', this.state);
          })
        }
      })
    };

    //save the image into the file list
    <Upload                                     
      listType="picture-card"
      fileList={fileList}
      onPreview={this.handlePreview}
      onChange={this.handleChange}
      {...props}
      >
      {fileList.length >= 8 ? null : uploadButton}
    </Upload>

    //Submit button
    <Button type="primary" 
        onClick={this.handleSubmit}
        loading={uploading}>
        Confirm
    </Button>

Here is my package version

"express": "^4.17.1",
"react": "^16.9.0",
FRANKfisher
  • 121
  • 3
  • 12
  • How are you sending the files in your front end? Can you include a snippet on how do you perform the request? – Carlos Martinez Nov 05 '19 at 03:04
  • Have a look at multer: https://github.com/expressjs/multer it's a middleware that supports multipart form requests in your express server. Note that your front end requests need to be a **multipart/form-data** request – Carlos Martinez Nov 05 '19 at 03:08
  • Also note this similar question https://stackoverflow.com/questions/49067423/multipart-form-data-post-method-using-express-js – Carlos Martinez Nov 05 '19 at 03:09

0 Answers0