【发布时间】:2021-10-23 22:59:34
【问题描述】:
我正在尝试设置 multer,但图像出现了一些问题,目前,当我不上传任何图像时,服务器会给我一个错误,不允许我继续。我希望即使图像未上传,仍将值存储在数据库中。
这是我配置 multer 的地方:
const express = require('express');
const router = express.Router();
const multer = require('multer');
const Clients = require('../models/clients.js')
const storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, "../emaildragger/public/uploads")
},
filename: (req, file, callback) => {
callback(null, file.originalname)
}
})
const upload = multer({storage: storage})
router.route('/').get((req, res) => {
Clients.find()
.then(client => res.json(client))
.catch(err => res.status(400).json('Error:' + err))
})
router.route('/:id').get((req, res) => {
Clients.findById(req.params.id)
.then(client => res.json(client))
.catch(err => res.status(400).json("Error" + err))
})
router.route('/add').post(upload.single("image"), (req, res) => {
const newClient = new Clients({
image: req.file.originalname,
firstName: req.body.firstName,
lastName: req.body.lastName,
weight: req.body.weight,
BMI: req.body.BMI
})
newClient.save()
.then (() => res.json(newClient))
.catch(err => res.status(400).json('error' + err))
})
module.exports = router
这是我的模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var clientsSchema = new Schema(
{
image:
{ type: String, required: false, default: 'Screenshot_293.png'},
firstName: { type: String, required: true },
lastName: { type: String, required: true },
weight: { type: String, required: false },
BMI: { type: String, required: false }
}
);
const Clients = mongoose.model("clients", clientsSchema)
module.exports = Clients
【问题讨论】:
-
你能把错误添加到问题中吗?
-
没有错误,只是没有任何反应
-
但是你有问题说服务器给你一个错误,不允许你继续。
-
POST http://localhost:4000/clients/add 500 (Internal Server Error)这是错误
标签: javascript node.js reactjs mongoose multer