Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

node.js - What is the problem in this script when I want to upload an image?

What is the problem in this script? When I want to upload an image I found this problem:

Error: ENOENT: no such file or directory, open 'C:UsershamzaDesktopMERN_Stack_Project_WeConnect_Social_Media-masterserverpublicuploadsprofile_pic1611529212065_0.jfif'
changeProfilePic(req, res) {
    let profile_pic = req.file.filename
    let loggedInUser = req.userDetails._id
    let reqUserId = req.body.id
    let oldPic = req.body.oldPic

    if (oldPic !== "user.png") {
        const filePath = `../server/public/uploads/profile_pic/${oldPic}`;
        fs.unlink(filePath, (err) => {
            if (err) { console.log(err) }
            if (reqUserId === loggedInUser) {
                userModel.findByIdAndUpdate(loggedInUser, {
                    profile_pic
                }).exec((err, result) => {
                    if (err) { console.log(err) }
                    res.json({ result })
                })
            } else {
                res.status(404)
            }
        })
    } else {
        if (reqUserId === loggedInUser) {
            userModel.findByIdAndUpdate(loggedInUser, {
                profile_pic
            }).exec((err, result) => {
                if (err) { console.log(err) }
                res.json({ result })
            })
        } else {
            res.status(404)
        }
    }
}
question from:https://stackoverflow.com/questions/65876973/what-is-the-problem-in-this-script-when-i-want-to-upload-an-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...