mongodbmongoosemongodb-compass

Document added with mongoose not showing in mongodb compass


I have tried adding document using mongoose. The following code in productController.js, returns the following response. Code:

exports.createProduct = async (req, res, next) => {
  try {
    console.log("Request Body:", req.body); // Log the request body for debugging
    const product = await Product.create(req.body);

    res.status(201).json({
      success: true,
      product,
    });
  } catch (error) {
    console.error("Error creating product:", error);
    res.status(400).json({
      success: false,
      message: error.message,
    });
  }
};

Response: Request Body: { name: 'Vaseline petrolatum Jelly', description: 'Simple product.', price: 100, rating: 3, category: 'Health', images: { public_id: 'sample public id', url: 'sample url' } } but I cannot seem to find it in compass.

I have checked the DB_URL etc. and checked to see if there are any faults but there do not seem to be any.


Solution

  • I had made a mistake in the DB_URL. I forgot to place the database name at the end of the string.

    DB_URL="mongodb://localhost:27017/Ecommerce"