TypeError:upload.single不是函数

我已经使用multer在node.js中上传文件,但是在设置服务器时出现了此错误。 TypeError:upload.single不是函数

这是我的server.js

const express=require('express');
const cors=require('cors');
const mongoose=require('mongoose');
const path = require('path');
const router = require('express').Router();
const multer = require('multer');
const upload = multer({dest:'images/'});


require('dotenv').config();

const app= express();
const port=process.env.PORT || 5000;

app.use(cors());
app.use(express.json());


//open connection to the database using Connection String used as an environment Variable
const uri = process.env.STORE_URI;
mongoose.connect(uri,{useNewUrlParser:true,useCreateIndex:true});


const connection=mongoose.connection;
connection.once('open',()=>{
console.log("MongoDB for Store connected successfully");
})
//

//Using the Routes for the Project in the Server
const productsRouter = require('./routes/Products.route');


app.use('/api/products',productsRouter);


if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'));

  app.get('*', (req,res)=>{
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  })
}

app.listen(port,()=>{
 console.log(`Server is runnig on port: ${port}`);
})

module.exports = upload;

这是我在服务器中使用的productRoute.js

const router = require('express').Router();
let Product = require('../models/product.model');
let upload = require('../server')

router.route('/add').post(upload.single('productImage'),(req,res)=>{

console.log(req.file);

const productname = req.body.productname;
const description = req.body.description;
const gender = req.body.gender;
const category = req.body.category;
const size = req.body.size;
const price = Number(req.body.price);
const quantity = Number(req.body.quantity);

const newProduct = new Product({
    productname,
    description,
    gender,
    category,
    size,
    price,
    quantity

})

newProduct.save()
    .then(()=>res.json("Product Added!"))
    .catch(err=>res.status(400).json('Err:'+err))
})

有人可以告诉我我在哪里犯了错误,以及对此问题的解决方案。提前致谢!!!

评论