Skip to main content

how fix error: MongoNotConnectedError: Client must be connected before running operations

HY all , I have a problem when I running this code:-

the file productinit.js

const product = require('../model/product');
const mongoose= require('mongoose');

const connectDB = async () => {
    await mongoose.connect('mongodb://127.0.0.1:27017/orders', {bufferTimeout: null});
    console.log('Connected to MongoDB');
  };
  connectDB();

const products = [
    new product({
        imagePath:"/images/about/a1.png",
        productName: "iphone 8+ ",
        price: 200,
        information:
        { 
        displaySize: 6.5,
        storageCapacity: 64,
        CameraResolution: 16,
        numberOfSIM: "Dual SIM"
        }}),

    new product({
        imagePath:"/images/about/a2.png",
        productName: "iphone 8 ",
        price: 240,
        information:
        { 
            displaySize: 5.5,
            storageCapacity: 64,
            CameraResolution: 16,
            numberOfSIM: "Dual SIM"
        }}),
 
     new product({
         imagePath:"/images/about/a3.png",
         productName: "iphone 7+ ",
         price: 180,
         information:
            { 
                displaySize: 6.1,
                storageCapacity: 64,
                CameraResolution: 15,
                numberOfSIM: "Dl SIM"
        }}),
    
    new product({
        imagePath:"/images/about/a4.png",
        productName: "iphone 7 ",
        price: 180,
        information:
                { 
                    displaySize: 6.1,
                    storageCapacity: 32,
                    CameraResolution: 15,
                    numberOfSIM: "Dl SIM"
        }}),
        
    new product({
            imagePath:"/images/about/a5.png",
            productName: "iphone 7+ ",
            price: 180,
            information:
            { 
                displaySize: 6.1,
                storageCapacity: 64,
                CameraResolution: 15,
                numberOfSIM: "Dl SIM"
        }}),

    new product({
                imagePath:"/images/about/a6.png",
                productName: "iphone 7+ ",
                price: 180,
                information:
                { 
                    displaySize: 6.1,
                    storageCapacity: 64,
                    CameraResolution: 15,
                    numberOfSIM: "Dl SIM"
        }}),
        
]

var done = 0;
mongoose.connect('mongodb://127.0.0.1:27017/orders');

for (var i = 0; i < products.length; i++) {
    console.log(i);
    var doc = products[i].save().then(() => console.log("Product saved")).catch(err => console.log(err));
    console.log(doc);
    done++;
    if (done === products.length) {
        mongoose.disconnect();
    }
}

the file product.js

const mongoose = require('mongoose');

const productSchema = mongoose.Schema({
    imagePath:{type:String, required:true},
    productName:{type:String, required:true},
    price:{type:Number, required:true},
    information:
    {
        required:true,
        type:
        {
            displaySize: Number,
            storageCapacity:Number,
            CameraResolution: Number,
            numberOfSIM: String
        }
    }
});


module.exports=mongoose.model('product',productSchema)

,display this error(how can fix it):

0
Promise { <pending> }
1
Promise { <pending> }
2
Promise { <pending> }
3
Promise { <pending> }
4
Promise { <pending> }
5
Promise { <pending> }
Connected to MongoDB
MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\utils.js:293:21)
    at executeOperation (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Collection.doQueue (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:128:23)
    at C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:75:24
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  [Symbol(errorLabels)]: Set(0) {}
}
MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\utils.js:293:21)
    at executeOperation (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Collection.doQueue (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:128:23)
    at C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:75:24
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  [Symbol(errorLabels)]: Set(0) {}
}
MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\utils.js:293:21)
    at executeOperation (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Collection.doQueue (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:128:23)
    at C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:75:24
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  [Symbol(errorLabels)]: Set(0) {}
}
MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\utils.js:293:21)
    at executeOperation (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Collection.doQueue (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:128:23)
    at C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:75:24
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  [Symbol(errorLabels)]: Set(0) {}
}
MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\utils.js:293:21)
    at executeOperation (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Collection.doQueue (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:128:23)
    at C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:75:24
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  [Symbol(errorLabels)]: Set(0) {}
}
MongoNotConnectedError: Client must be connected before running operations
    at executeOperationAsync (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:23:19)
    at C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:45
    at maybeCallback (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\utils.js:293:21)
    at executeOperation (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\operations\execute_operation.js:12:38)
    at Collection.insertOne (C:\courses\node-js\shopping-cart\node_modules\mongodb\lib\collection.js:157:57)
    at NativeCollection.<computed> [as insertOne] (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:243:33)
    at Collection.doQueue (C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:128:23)
    at C:\courses\node-js\shopping-cart\node_modules\mongoose\lib\collection.js:75:24
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  [Symbol(errorLabels)]: Set(0) {}
}

if i use 'mongodb://localhost:27017/orders'instead of'mongodb://127.0.0.1:27017/orders'

,the error will be:

MongooseError: Operation `products.insertOne()` buffering timed out after 10000ms at Timeout

err = new ServerSelectionError(); ^ MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

Via Active questions tagged javascript - Stack Overflow https://ift.tt/1ft4IKP

Comments

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console