Skip to content

When creating the post without entering the fields like title, summary or content it shows errors in the backend  #5

@mhd101

Description

@mhd101

error2

This error occured because we are directly destructuring the original name from file input, as we have not uploaded the file. To fix this issue we can first check the req.file === undefined if true then status 400 else destructure the original name from the file input.

Here is the fix code

if(req.file === undefined){
        res.status(400).json('error')
    } else {
        const { originalname, path } = req.file
        const { token } = req.cookies;
        const parts = originalname.split('.')
        const ext = parts[parts.length - 1]
        const newPath = path + '.' + ext
        fs.renameSync(path, newPath)
    
        const { title, summary, content } = req.body
    
        jwt.verify(token, secret, {}, async (err, info) => {
            if (err) throw err;
            const postDoc = await Post.create({
                title: title,
                summary: summary,
                cover: newPath,
                content: content,
                author: info.id
            })
            res.json(postDoc)
    
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions