I have node module that exports two function:
const cloudinary = require('cloudinary').v2;
const config = require('config');
const winston = require('winston');
cloudinary.config({
secure: true,
cloud_name: 'loofy',
api_key: config.get('cloudinary_api_key'),
api_secret: config.get('cloudinary_secret')
});
const uploadImage = async (localImage, optionsIn) => {
const options = {
use_filename: true,
unique_filename: false,
overwrite: true
};
if (optionsIn) {
Object.keys(optionsIn).forEach((key) => {
options[key] = optionsIn[key];
});
}
try {
const result = await cloudinary.uploader.upload(localImage, options);
return result;
} catch (error) {
winston.warn(error.message);
return null;
}
};
const destroyImage = async(public_id) => {
const result = await cloudinary.uploader.destroy(public_id);
return result;
};
exports.uploadImage = uploadImage;
exports.destrοyImage = destroyImage
In another module I am importing it:
const express = require('express');
const router = express.Router();
const winston = require('winston');
const fs = require('fs');
const path = require("path");
const validateOID = require('../middleware/validateObjectId');
const auth = require('../middleware/auth');
const authadmin = require('../middleware/authadmin');
const uploadMid = require('../middleware/upload');
const {User, updateUser, createNewUser, validateSearch} = require('../models/users');
const {uploadImage, destroyImage} = require('../models/cloudinaryUploader');
const images = require('../models/images');
router.post('/', uploadMid, /*[auth, authadmin],*/ async (req, res) => {
const pathToDir = path.join(__dirname, "../uploads/images");
createUploadDir(pathToDir);
res.setHeader('Access-Control-Allow-Origin', '*');
let newUser;
try {
newUser = await createNewUser(req.body);
}
catch(err) {
winston.warn(err.message);
res.status(500).send(err.message);
}
const imageData = await uploadImage(req.userImage, {upload_preset:'loofy_batch'});
let loofyImageData = images.loofyTransform(imageData);
loofyImageData.albums = ['_profile_album'];
loofyImageData.tags = [];
try {
newUser.addUserImage(loofyImageData);
newUser.setProfileImage(imageData.asset_id);
// newUser.setCoverImage(imageData.asset_id);
newUser.save();
}
catch(err) {
winston.warn(err.message);
return res.status(400).send(err.message);
}
if(req.userImage) {
fs.unlinkSync(req.userImage);
}
res.status(200).send( reformUserData(newUser) );
});
router.delete('/:id/images/:asset_id', /*auth,*/ async (req, res) => {
const asset_id = req.params.asset_id;
let user = await User.findOne({_id: req.params.id});
if (!user) return res.status(400).send('Δεν βρέθηκε ο χρήστης');
const image2Del = await user.images.images.find((item) =>{
return item.asset_id === asset_id;
});
if(!image2Del) return res.status(400).send('Δεν βρέθηκε η εικόνα');
try {
const public_id = image2Del.public_id;
await destroyImage(public_id);
if (user.images.profile === asset_id) {
user.images.profile = '';
}
await user.images.images.pull({public_id: public_id});
user.save();
}
catch(err) {
return res.status(500).send(err.message);
}
return res.status(204).send('Το αρχείο διαγράφηκε επιτυχώς');
});
At router.post the call to
await uploadImage(req.userImage, {upload_preset:'loofy_batch'});
is working.
At router.delete the call to
await destroyImage(public_id);
throws error because destroyImage is undefined. I am quite new to Node and I struggling two days. First i thought it was the way I exported the code, but I am doing it the same way elsewhere and it is working. By inspecting exports at the first module I see that both are not undefined.
Any help is appreciated.
Just replace this
exports.uploadImage = uploadImage;
exports.destrοyImage = destroyImage
With
module.exports = {
uploadImage,destroyImage
}