Getting Internal Server Error (HTTP 500) when Trying to Query Smart Bin by binId in Node.js Application

I’m encountering an issue with my Node.js application, which serves as an API for querying smart bins. I recently updated the route to allow querying by binId instead of id, and I’m now facing an “Internal Server Error” (HTTP 500) when making GET requests.

Here’s what my route and controller look like:

Route (Express.js):

router.get('/:binId', smartBinController.getSmartBinByBinId);

Controller (Node.js):

const getSmartBinByBinId = async (req, res) => {
  try {
    const { binId } = req.params;
    const { binLevel, binOrientation, binLocation, binName } = req.query;

    const smartBin = await SmartBin.getSmartBinByBinId(binId);

    if (!smartBin) {
      return res.status(404).json({ error: 'Smart bin not found' });
    }

    if (binLevel) {
      smartBin.binLevel = binLevel;
    }
    if (binOrientation) {
      smartBin.binOrientation = binOrientation;
    }
    if (binLocation) {
      smartBin.binLocation = binLocation;
    }
    if (binName) {
      smartBin.binName = binName;
    }

    await smartBin.save();

    res.status(200).json({ message: 'Smart bin updated successfully', smartBin });
  } catch (error) {
    console.error('Error updating smart bin:', error);
    res.status(500).json({ error: 'Failed to update smart bin' });
  }
};

Model:

const smartBinSchema = new mongoose.Schema({
  binId: {
    type: String,
    required: true,
    unique: true,
  },
  binLevel: {
    type: Number,
    required: true,
  },
  binOrientation: {
    type: String,
    required: true,
  },
  binLocation: {
    type: String,
    required: true,
  },
  binName: {
    type: String,
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
  updatedAt: {
    type: Date,
    default: Date.now,
  },
});


const getSmartBinByBinId = async (binId) => {
  try {
    const smartBin = await SmartBin.findOne({ binId });
    return smartBin;
  } catch (error) {
    throw error;
  }
};

module.exports = {
  getSmartBinByBinId,
};

It’s works well with querying by id:
https://iot-smart-bin.onrender.com/api/smartbin/64a9a7d7fbde3940ba8f132f?binId=bin001&binLevel=90&binOrientation=Upright&binLocation=Kuje%prison&binName=Kuje%Prison.

Querying by binId that’s not working: https://iot-smart-bin.onrender.com/api/smartbin/bin001?binLevel=80&binOrientation=Upright&binLocation=Kuje%prison&binName=Kuje%Prison

Github Link: https://github.com/blcdevs/smart-iot-bin

I’ve checked my server logs, but the error message is not very descriptive. I suspect there might be an issue in my route, controller, or possibly with my MongoDB database. Can anyone help me diagnose and resolve this “Internal Server Error” issue?

Any insights, suggestions, or debugging tips would be greatly appreciated!