image not found error come when i try to update tiltle and discription field only

abhi67
abhi67 Member Posts: 4

i got this error : error in blog post: {

  message: 'Resource not found - https://res.cloudinary.com/drv2gu8b9/image/upload/v1700847934/blog-mern-app/kzg1lxdqfnt2asls0mgq.jpg',

 name: 'Error',

 http_code: 404

}

this is my create post controller:

const createPostController = async (req, res) => {

    try {

        const { title, description, image } = req.body;

        if(!title){

            return res.status(400).json({message:"invalid title"});

        }

        if(!description){

            return res.status(400).json({message:"invalid description"});

        }

        if(!image){

            return res.status(400).json({message:"invalid image"});

        }

        const uploadPhoto = await cloudinary.uploader.upload(image, { folder: "blog-mern-app" });

        const createPost = new Post({ title, description, image: { public_id: uploadPhoto.public_id, url: uploadPhoto.secure_url } });

        await createPost.save();

        res.status(201).json({success:true, message:"post created successully", createPost});

    } catch (error) {

        res.status(500).json({success:false, message: "Error Creating The Blog post"})

    }

}

this is my update post controller

const updatePostController = async (req, res) => {

    try {

        const { title, description, image } = req.body;

        const currentPost = await Post.findById(req.params.id);


        if (!currentPost) {

            return res.status(404).json({ message: "Post not found" });

        }


        if(!image){

            return res.status(404).json({message:"image is compulsary"});

        }


        if (image) {

            if (currentPost.image) {

                const currentImgId = currentPost.image.public_id;

                await cloudinary.uploader.destroy(currentImgId);

            }

           

            const uploadNewImg = await cloudinary.uploader.upload(image, { folder: "blog-mern-app" });

            currentPost.image = {public_id:uploadNewImg.public_id, url:uploadNewImg.secure_url};

        }

        currentPost.title = title;

        currentPost.description = description;


        await currentPost.save();

        res.status(201).json({ success:true, message:"updated post successfully", currentPost});

    } catch (error) {

        console.error("error in blog post:", error);

        res.status(500).json({success:false, message: "error in updating blog post", error:error.message });

    }

}

Tagged:

Answers

  • Zachary
    Zachary Member, Cloudinary Staff Posts: 46

    Hey there,

    Looking at that URL I see the resource doesn't exist in your account. Looking at our logs I see it was deleted November 24th 2023, 17:45 UTC.

    I hope that helps. Please let me know if trying this with a different resource still results in any problems.

    -Zachary

  • abhi67
    abhi67 Member Posts: 4

    thanks for the information. but then what is wrong in my code can you help me regarding this ?

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 150

    Hey @abhi67 .

    Looking at our logs, I see that "https://res.cloudinary.com/drv2gu8b9/image/upload/v1700847934/blog-mern-app/kzg1lxdqfnt2asls0mgq.jpg" was passed as the image param in an upload, but as my colleague Zachary pointed out, this doesn't exist. Where is this URL coming from before it is being passed into the req.body ?

    I would recommend adding some additional logging, and also checking that the URL passed in req.body.image is reachable before trying to run cloudinary.uploader.upload()

    Kind regards,

    -Danny

  • abhi67
    abhi67 Member Posts: 4

    Ok thanks for the error checking . Let me try 🙂

  • abhi67
    abhi67 Member Posts: 4

    thanks , finally i found the error .

    the error is coming from my server side code . when i try to update text or description only somehow image is deleted also, so this is the reason. after updating my updatePostController it is working fine.

    thanks DannyFromCloudinary and Zachary for helping me out.

    const updatePostController = async (req, res) => {

        try {

            const { title, description, image } = req.body;

            const currentPost = await Post.findById(req.params.id);


            if (!currentPost) {

                return res.status(404).json({ message: "Post not found" });

            }


            if (image && image !== currentPost.image?.url) {

                // Delete the old image if a new image is provided and it's different

                const currentImgId = currentPost.image.public_id;

                await cloudinary.uploader.destroy(currentImgId);


                // Upload the new image

                const uploadNewImg = await cloudinary.uploader.upload(image, { folder: "blog-mern-app" });

                currentPost.image = { public_id: uploadNewImg.public_id, url: uploadNewImg.secure_url };

            }


            // Update title and description only if provided in the request body

            if (title !== undefined) {

                currentPost.title = title;

            }


            if (description !== undefined) {

                currentPost.description = description;

            }


            await currentPost.save();

            res.status(201).json({ success: true, message: "Updated post successfully", currentPost });

        } catch (error) {

            console.error("Error in blog post:", error);

            res.status(500).json({ success: false, message: "Error in updating blog post", error: error.message });

        }

    };