简体   繁体   中英

"Cannot read properties of undefined (reading '0')

i am making guide's update controller which inlude name email password avatar[image] and guidelicence[pdf].It gives the error cannot read undefined at(0) but same logic runs for signup.I am using multer and cloudinary for form data,been stuck for 2 days. Update Route

routes.put(
  "/:userID",
  upload.fields([
    { name: "avatar", maxCount: 1 },
    { name: "guidelicense", maxCount: 1 },
  ]),
  update
);

Update Controller

exports.update = async (req, res) => {
  try {
    // handle the form data
    const {
      fullname,
      password,
      email,
      address,
      isAvalaible,
      phonenumber,
      cnic,
    } = req.body;
    // handle the image file
    const avatar = req.files.avatar[0];
    console.log(req.files.avatar[0]);
    // handle the PDF file
    const guideLicense = req.files.guidelicense[0];
    console.log(req.files.guidelicense[0]);

    // upload the image to Cloudinary
    cloudinary.uploader.upload(
      avatar.path,
      { resource_type: "image" },
      (err, image) => {
        if (err) throw err;
        // store the image URL in the database
        const avatarUrl = image.url;

        // upload the PDF to Cloudinary
        cloudinary.uploader.upload(
          guideLicense.path,
          { resource_type: "raw" },
          (err, file) => {
            if (err) throw err;
            // store the PDF URL in the database
            const guideLicenseUrl = file.url;
            // update the guide's information in the database
            Guides.findOneAndUpdate(
              { email },
              {
                fullname,
                password,
                avatarUrl,
                guideLicenseUrl,
                address,
                isAvalaible,
                phonenumber,
                cnic,
              },
              (err, guide) => {
                if (err) throw err;
                res.status(200).json({ message: "Guide updated successfully" });
              }
            );
          }
        );
      }
    );
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

multer configuration

if (!fs.existsSync("./uploads")) {
  fs.mkdirSync("./uploads");
}

// Multer config
module.exports = multer({
  storage: multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, "./uploads");
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname);
    },
  }),

  limits: { fileSize: 1024 * 1024 * 5 },
  fileFilter: (req, file, cb) => {
    let ext = path.extname(file.originalname);
    if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png" && ext !== ".pdf") {
      cb(new Error("File type is not supported"), false);
      return;
    }
    cb(null, true);
  },
});

i wanted to get guide updated successfully but got

{
    "message": "Cannot read properties of undefined (reading '0')"
}

error

You need to parse the req before accessing it. Use the parsed "request" to access the data.

const request = JSON.parse(req);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM