TypeError:upload.single不是一个函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
这是我在服务器中使用的我的productsRoute.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))
)
有人可以告诉我我在哪里犯了错误,以及对此问题的解决方案。在此先感谢!
答案
我测试您的代码,您应该在module.exports = upload;
之前添加以下代码行:const productsRouter = require('./productsRoute');
,并将module.exports = router;
添加到productsRoute.js:
server.js:
const express=require('express');
const cors=require('cors');
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());
/*change this line*/
module.exports = upload;
//Using the Routes for the Project in the Server
const productsRouter = require('./productsRoute');
app.use('/api/products', productsRouter);
app.listen(port,()=>
console.log(`Server is runnig on port: $port`);
)
productsRoute.js:
const router = require('express').Router();
//let Product = require('../models/product.model');
let upload = require('./index')
//const express = require('express');
//const router = new express.Router();
router.route('/add').post(upload.single('productImage'),(req,res)=>
//router.post("/add", 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
)
);
module.exports = router;
以上是关于TypeError:upload.single不是一个函数的主要内容,如果未能解决你的问题,请参考以下文章
TypeError:“TypeError:函数名称不是 HTMLButtonElement.onclick (/:2:54) 处的函数”