Home > Blockchain >  Express ReferenceError: navigator is not defined
Express ReferenceError: navigator is not defined

Time:01-03

On running backend, I am getting the following error

C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:1168 var useAgent = navigator && navigator.userAgent || ''; ^

ReferenceError: navigator is not defined at Module../src/namespace/cloudinary-core.js (C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:1168:16) at webpack_require (C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:37:30) at C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:101:18 at C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:104:10 at webpackUniversalModuleDefinition (C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:10:20) at Object. (C:\Users\Karan\Projects\MERN\reddit-clone\node_modules\cloudinary-core\cloudinary-core.js:17:3) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12)

I am using cloudinary to upload image.

config.js

require('dotenv').config();
const cloudinary = require('cloudinary').v2;

const PORT = process.env.PORT;
const MONGODB_URI = process.env.MONGODB_URI;
const SECRET = process.env.SECRET;
const UPLOAD_PRESET = process.env.UPLOAD_PRESET;

cloudinary.config({
    cloud_name: process.env.CLOUDINARY_NAME,
    api_key: process.env.CLOUDINARY_API_KEY,
    api_secret: process.env.CLOUDINARY_API_SECRET,
});

module.exports = {
    PORT,
    MONGODB_URI,
    SECRET,
    cloudinary,
    UPLOAD_PRESET,
};

One of the function using cloudinary, from which i am pretty sure not getting this error

user.js

const setUserAvatar = async (req, res) => { const { avatarImage } = req.body;

if (!avatarImage) {
  return res
    .status(400)
    .send({ message: 'Image URL needed for setting avatar.' });
}

const user = await User.findById(req.user);

if (!user) {
  return res
    .status(404)
    .send({ message: 'User does not exist in database.' });
}

const uploadedImage = await cloudinary.uploader.upload(
  avatarImage,
  {
    upload_preset: UPLOAD_PRESET,
  },
  (error) => {
    if (error) return res.status(401).send({ message: error.message });
  }
);

user.avatar = {
  exists: true,
  imageLink: uploadedImage.url,
  imageId: uploadedImage.public_id,
};

const savedUser = await user.save();
res.status(201).json({ avatar: savedUser.avatar }); };

Can somebody explain what is this error and how can i fix it? Also, I am using the latest cloudinary package.

CodePudding user response:

Cloudinary had just released a new version to its JS SDK (version 2.12.2) that fixes this issue.

  • Related