0

I’m new to learning MERN stack and node js. However, I have been struggling with the Mongoose error for a long while. Hopefully, some can help me out!

MongoDB is connected. enter image description here

Here is the config.env:

PORT = 4000
NODE_ENV = DEVELOPMENT

DB_LOCAL_URI = mongodb://localhost:27017/shopit

Here is the database.js:

const mongoose = require('mongoose');

const connectDatabase = async () => {
  try {
    await mongoose.connect(process.env.DB_LOCAL_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log(`MongoDB Database connected with HOST: ${mongoose.connection.host}`);
  } catch (error) {
    console.error('MongoDB connection error:', error);
  }
};

module.exports = connectDatabase;

Here’s the server.js:

const app = require('./app')
const connectDatabase = require('./config/database')


const dotenv = require('dotenv');

// Setting up config file
dotenv.config({ path: 'backend/config/config.env' })

// Connecting to database
connectDatabase();

app.listen(process.env.PORT, () => {
    console.log(`Server started on PORT: ${process.env.PORT} in ${process.env.NODE_ENV} mode.`)
})

This is the model, prduct.js:

const mongoose = require('mongoose')

const productSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please enter product name'],
        trim: true,
        maxLength: [100, 'Product name cannot exceed 100 characters']
    },
    price: {
        type: Number,
        required: [true, 'Please enter product price'],
        maxLength: [5, 'Product name cannot exceed 5 characters'],
        default: 0.0
    },
    description: {
        type: String,
        required: [true, 'Please enter product description'],
    },
    ratings: {
        type: Number,
        default: 0
    },
    images: [
        {
            public_id: {
                type: String,
                required: true
            },
            url: {
                type: String,
                required: true
            },
        }
    ],
    category: {
        type: String,
        required: [true, 'Please select category for this product'],
        enum: {
            values: [
                'Electronics',
                'Cameras',
                'Laptops',
                'Accessories',
                'Headphones',
                'Food',
                "Books",
                'Clothes/Shoes',
                'Beauty/Health',
                'Sports',
                'Outdoor',
                'Home'
            ],
            message: 'Please select correct category for product'
        }
    },
    seller: {
        type: String,
        required: [true, 'Please enter product seller']
    },
    stock: {
        type: Number,
        required: [true, 'Please enter product stock'],
        maxLength: [5, 'Product name cannot exceed 5 characters'],
        default: 0
    },
    numOfReviews: {
        type: Number,
        default: 0
    },
    reviews: [
        {
            user: {
                type: mongoose.Schema.ObjectId,
                ref: 'User'
            },
            name: {
                type: String,
                required: true
            },
            rating: {
                type: Number,
                required: true
            },
            comment: {
                type: String,
                required: true
            }
        }
    ],
    user: {
        type: mongoose.Schema.ObjectId,
        ref: 'User'
    },
    createdAt: {
        type: Date,
        default: Date.now
    }
})

module.exports = mongoose.model('Product', productSchema);

Here’s the controller.

const Product = require('../models/product')

// Create new product => /api/v1/product/new
exports.newProduct = async(req, res, next) => {

    const product = await Product.create(req.body);

    res.status(201).json({
        success: true,
        product
    })
}
exports.getProducts = (req, res, next) => {
    res.status(200).json({
        success: true,
        message: 'This route will show all products in database.'
    })
}

The error is : MongooseError: Operation products.insertOne() buffering timed out after 10000ms at Timeout. (C:\Users\Xin\Documents\SHOPIT\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:153:23) at listOnTimeout (node:internal/timers:564:17) at process.processTimers (node:internal/timers:507:7)

Phyllis
  • 1
  • 1
  • Does this answer your question? [MongooseError - Operation \`users.findOne()\` buffering timed out after 10000ms](https://stackoverflow.com/questions/65408618/mongooseerror-operation-users-findone-buffering-timed-out-after-10000ms) – ayrusme Jun 07 '23 at 21:41

0 Answers0