Getting undefined result of cloudinary.uploader.upload
I am trying to get the secure_url of the respones that i am getting from cloudinary.uploader.upload but it says undefined
Here is my backend code using nodejs:-
const updateUser = async(req,res) => {
const {name, email, username, password, bio} = req.body;
let { profilePic }= req.body
const userId = req.user._id;
try {
let user = await User.findById(userId);
if(!user){
return res.status(400).json({error: 'User not found'});
}
if (req.params.id !== userId.toString())
return res.status(400).json({ error: "You cannot update other user's profile" })
if(password){
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password,salt);
user.password = hashedPassword
}
if (profilePic) {
if (user.profilePic) {
await cloudinary.uploader.destroy(user.profilePic.split("/").pop().split(".")[0]);
}
const uploadedResponse = await cloudinary.uploader.upload(profilePic);
profilePic = uploadedResponse.secure_url;
}
user.name = name || user.name
user.email = email || user.email
user.username = username || user.username
user.bio = bio || user.bio
user.profilePic = profilePic || user.profilePic
user = await user.save();
res.status(200).json({message: 'Profile updated successfully', user});
} catch (err) {
res.status(500).json({error: err.message})
console.log("Error in updateProfile: ", err.message)
}
}
-
Official comment
Hello,
This support forum is no longer in use. Please use the Cloudinary Community at https://community.cloudinary.com/ to receive assistance from other members of the community and from Cloudinary support team. For account-specific questions or if you believe that you've encountered a bug, please contact the Cloudinary team directly via the "submit a request" option on this support site.
For this specific issue, I would suggest logging the entire response body from the uploader.upload request to see if the secure_url exists there. If it does, then log it after each step where you are modifying the URL and this should help discover the issue. If you are unable to find the issue after these steps, please open a support ticket and we will be able to further assist.
Post is closed for comments.
Comments
1 comment