next.js 如果req.file未定义,则express崩溃

taor4pac  于 2023-01-05  发布在  其他
关注(0)|答案(2)|浏览(137)

当发出一个post请求到一个express API没有一个附加的文件在这请求中,api崩溃并且提供TypeError: Cannot read properties of undefined (reading 'filename')错误.然而我想使它这样api不崩溃当一个post请求是没有一个附加的图像.任何想法?
快递编码:

const storage = multer.diskStorage({
    destination: (req, res, cb) => {
        cb(null, dir)
    },
    filename: (req, file, cb) => {
        cb(null, Date.now() + file.originalname)
    }
})

const upload = multer({
    storage: storage
})

router.get('/', async (req, res) => {
    try {
        const members = await Member.find();
        res.json(members);
    } catch (err) {
        res.status(500).json({ message: err.message });
    }
})
router.get('/:id', getMember, async (req, res) => {
    res.json(res.member)
})
router.post('/', upload.single('image'), async (req, res) =>{
    const member = new Member({
        name: req.body.name,
        occupation: req.body.occupation,
        bio: req.body.bio,
        join: req.body.join,
        image: req.file.filename
    })
    try {
        const newMember = await member.save()
        res.status(201).json(newMember)
    } catch (err) {
        res.status(400).json({ message: err.message });
    }
})

实际发送文件的nextjs代码:

const submitHandler = (e) => {
        e.preventDefault()
            const formDatas = new FormData()
            formDatas.append('name', name)
            formDatas.append('occupation', occupation)
            formDatas.append('bio', paragraph)
            formDatas.append('join', date)
            formDatas.append('image', img)
        console.log(formDatas)
        axios
        .post(api + '/members', formDatas)
        .then(res => console.log(res))
        .catch(err => console.log(err))
    }
4uqofj5v

4uqofj5v1#

由于您试图访问未定义变量req.file的对象,因此出错;
您可以根据需要进行更改
1如果您不想接受没有任何文件的请求

router.post('/', upload.single('image'), async (req, res) => {
if (!req.file) { //or you can check if(req.file===undefiend)
    return res.status(400).json({ message: 'Please attach a file' });
}
const member = new Member({
    name: req.body.name,
    occupation: req.body.occupation,
    bio: req.body.bio,
    join: req.body.join,
    image: req.file.filename
})
try {
    const newMember = await member.save()
    res.status(201).json(newMember)
} catch (err) {
    res.status(400).json({ message: err.message });
}

})
2如果您想存储null/空字符串(如果没有文件上传)

router.post('/', upload.single('image'), async (req, res) => {
const member = new Member({
    name: req.body.name,
    occupation: req.body.occupation,
    bio: req.body.bio,
    join: req.body.join,
    image: req.file!==undefined ? req.file.filename : null
})
try {
    const newMember = await member.save()
    res.status(201).json(newMember)
} catch (err) {
    res.status(400).json({ message: err.message });
}

})

cwtwac6a

cwtwac6a2#

更改如下所示

const member = new Member({
        name: req.body.name,
        occupation: req.body.occupation,
        bio: req.body.bio,
        join: req.body.join,
        image: req.file.filename ? req.file.filename : ""
    })

这应该停止崩溃,但直到我看到整个代码不会知道你是如何处理错误

相关问题