在我的src目录中,有一个临时文件,使用multer我上传临时文件中的文件,但无法上传cloudinary中的文件。为什么会这样请帮助我,请帮助我
这是我的代码:
console.log("file detail:", JSON.stringify(req.file))
//output {"fieldname":"avatar","originalname":"Screenshot 2024-07-27 004034.png","encoding":"7bit","mimetype":"image/png","destination":"uploads/","filename":"Screenshot 2024-07-27 004034.png","path":"uploads\Screenshot 2024-07-27 004034.png","size":474631}
if (req.file) {
try {
const result = await cloudinary.v2.uploader.upload(req.file.path, {
folder: "learnify",
width: 250,
height: 250,
gravity: "faces",
crop: "fill",
}).then(result=>console.log('RESULT',result)).catch(error => {
console.error(error.message)});// For result i get undefined
if (result) {
user.avatar.public_id = result.public_id;
user.avatar.secure_url = result.secure_url;
// Remove file from server
fs.rm(`../temp/${req.file.filename}`);
}
} catch (error) {
return next(new AppError("File not upload, please try again", 500));
}
我正在尝试将文件从临时文件上传到 cloudnary...
@harshit,完整的错误日志是什么?
您提到了
For result i get undefined
,但应该还有更多内容。你能发布完整的错误吗?
如果您遇到这样的情况
TypeError: Cannot read properties of undefined (reading 'uploader')
,则说明导入有问题。
如果您将其导入为
require('cloudinary').v2
,则必须在通话中省略 v2
- await cloudinary.uploader.upload
。