cloudinary / cloudinary_npm

Cloudinary NPM for node.js integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Cannot read properties of undefined (reading 'upload')

quangnmwork opened this issue · comments

commented

I'm trying to upload image with Nodejs+ typescript .
I have do like the tutorial . Here is my config .

utils/cloudinary.ts

import { v2 as Cloudinary } from "cloudinary";
const config = {
  cloud_name: "xxx",
  api_key: "xxx",
  api_secret: "xxx",
};

export default cloudinary;

In userController.ts

export class UserController {
  static async editProfile(
    req: MulterRequest,
    res: Response,
    next: NextFunction
  ) {
    // Filter object want to update
    const updateUser: Partial<
      Pick<IUser, "avatar" | "username" | "location" | "description">
    > = {
      username: "",
      avatar: "",
      location: "",
      description: "",
    };
    console.log(req.file.path);
    console.log(cloudinary.config);
    if (req.file.path) {
      await cloudinary.uploader.upload(
        "https://upload.wikimedia.org/wikipedia/commons/a/ae/Olympic_flag.jpg",
        { public_id: "olympic_flag" },
        function (error, result) {
          console.log(result);
        }
      );
    }
    Object.keys(updateUser).forEach((objKey) => {
      if (!req.body[objKey] && objKey !== "avatar") delete updateUser[objKey];
    });
    // console.log(updateUser);

    res.status(200).send(updateUser);
  }
}

After the function upload of cloudinary run . I got the error like this :

TypeError: Cannot read properties of undefined (reading 'upload')
    at D:\IT-Nihongo-BE\src\controllers\UserController.ts:37:33
    at step (D:\IT-Nihongo-BE\src\controllers\UserController.ts:33:23)
    at Object.next (D:\IT-Nihongo-BE\src\controllers\UserController.ts:14:53)
    at D:\IT-Nihongo-BE\src\controllers\UserController.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (D:\IT-Nihongo-BE\src\controllers\UserController.ts:4:12)
    at UserController.editProfile (D:\IT-Nihongo-BE\src\controllers\UserController.ts:70:16)
    at Layer.handle [as handle_request] (D:\IT-Nihongo-BE\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\IT-Nihongo-BE\node_modules\express\lib\router\route.js:144:13)
    at done (D:\IT-Nihongo-BE\node_modules\multer\lib\make-middleware.js:45:7)

I really don't understand this error. Please help

Hey Quang,

Please open a support ticket with us support@cloudinary.com. Also, please refrain from posting your api credentials on a public board for security reasons. We removed it from the post here, but we suggest creating a new set of keys and disabling the current API credentials since they were posted publicly.

commented

Sorry . I forgot to delete it .

No problem, did you send in a request to our support team? We may need to ask for more information on this one, so it would be best if we did it in a direct line of communication!

commented

Sure , I think I will send request to cloudinary support as soon as I can . But now I have to use another one instead . I will close issue now .