Home > Software engineering >  TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is
TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is

Time:10-17

I have a React application with a Node.js backend.

I have this route on the backend:

router.put(
  '/testimonial/:testimonialId/:action',
  [authenticate, validateUser],
  async (req, res) => {
    if (req.params.action === 'ACCEPT') {
      const { err, status, data } =
        await notificationController.acceptTestimonial(
          req.params.testimonialId,
          req.user._id
        );
      res.status(status).send({ err, data });
    } else if (req.params.action === 'REJECT') {
      const { err, status, data } =
        await notificationController.rejectTestimonial(
          req.params.testimonialId,
          req.user
        );
      res.status(status).send({ err, data });
    }
  }
);

These are the corresponding Controllers: (acceptTestimonial/rejectTestimonial)

const acceptTestimonial = async (testimonialId, userId) => {
  try {
    await Users.findOne({ _id: userId }, async function (err, creator) {
      if (err) return { err, status: 404, data: null };
      else {
        const updatedTestimonials = creator.testimonials.map((testimonial) => {
          if (testimonial._id == testimonialId) {
            testimonial.status = 'PUBLISHED';
          }
          return testimonial;
        });

        creator.testimonials = updatedTestimonials;
        await creator.save();
        return { err: null, status: 200, data: creator };
      }
    });
  } catch (err) {
    return { err: err.toString(), status: 500, data: null };
  }
};
const rejectTestimonial = async (testimonialId, userId) => {
  try {
    await Users.findOne({ _id: userId }, async function (err, creator) {
      if (!creator)
        return { err: 'Creator not found!', status: 404, data: null };
      else {
        const updatedTestimonials = creator.testimonials.map((testimonial) => {
          if (testimonial._id === testimonialId) {
            testimonial.status = 'REJECTED';
          }
          return testimonial;
        });
        creator.testimonials = updatedTestimonials;

        await creator.save();
        return { err: null, status: 200, data: creator };
      }
    });
  } catch (err) {
    return { err: err.toString(), status: 500, data: null };
  }
};

When this route runs and performs the operations, I get this error in the console

UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined.

I'm unable to figure out what the problem is, is there a problem with destructuring or passing values ?

Thanks in advance.

CodePudding user response:

it is because, you aren't "returning" anything in try block of your controllers. The return statement is actually of callback function.

await is used to replace callback & promise.then so you have to return something in try block. You can achieve this like:

const rejectTestimonial = async (testimonialId, userId) => {
    try {
        // await Users.findOne({ _id: userId }, async function (err, creator) {
        //   if (!creator)
        //     return { err: 'Creator not found!', status: 404, data: null };
        //   else {
        //     const updatedTestimonials = creator.testimonials.map((testimonial) => {
        //       if (testimonial._id === testimonialId) {
        //         testimonial.status = 'REJECTED';
        //       }
        //       return testimonial;
        //     });
        //     creator.testimonials = updatedTestimonials;
    
        //     await creator.save();
        //     return { err: null, status: 200, data: creator };
        //   }
        // });

        //            
  • Related