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:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:23:19)
at C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:45
at maybeCallback (C:coursesnode-jsshopping-cartnode_modulesmongodblibutils.js:293:21)
at executeOperation (C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:38)
at Collection.insertOne (C:coursesnode-jsshopping-cartnode_modulesmongodblibcollection.js:157:57)
at NativeCollection.<computed> [as insertOne] (C:coursesnode-jsshopping-cartnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:243:33)
at Collection.doQueue (C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.js:128:23)
at C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.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:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:23:19)
at C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:45
at maybeCallback (C:coursesnode-jsshopping-cartnode_modulesmongodblibutils.js:293:21)
at executeOperation (C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:38)
at Collection.insertOne (C:coursesnode-jsshopping-cartnode_modulesmongodblibcollection.js:157:57)
at NativeCollection.<computed> [as insertOne] (C:coursesnode-jsshopping-cartnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:243:33)
at Collection.doQueue (C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.js:128:23)
at C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.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:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:23:19)
at C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:45
at maybeCallback (C:coursesnode-jsshopping-cartnode_modulesmongodblibutils.js:293:21)
at executeOperation (C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:38)
at Collection.insertOne (C:coursesnode-jsshopping-cartnode_modulesmongodblibcollection.js:157:57)
at NativeCollection.<computed> [as insertOne] (C:coursesnode-jsshopping-cartnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:243:33)
at Collection.doQueue (C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.js:128:23)
at C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.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:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:23:19)
at C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:45
at maybeCallback (C:coursesnode-jsshopping-cartnode_modulesmongodblibutils.js:293:21)
at executeOperation (C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:38)
at Collection.insertOne (C:coursesnode-jsshopping-cartnode_modulesmongodblibcollection.js:157:57)
at NativeCollection.<computed> [as insertOne] (C:coursesnode-jsshopping-cartnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:243:33)
at Collection.doQueue (C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.js:128:23)
at C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.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:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:23:19)
at C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:45
at maybeCallback (C:coursesnode-jsshopping-cartnode_modulesmongodblibutils.js:293:21)
at executeOperation (C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:38)
at Collection.insertOne (C:coursesnode-jsshopping-cartnode_modulesmongodblibcollection.js:157:57)
at NativeCollection.<computed> [as insertOne] (C:coursesnode-jsshopping-cartnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:243:33)
at Collection.doQueue (C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.js:128:23)
at C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.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:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:23:19)
at C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:45
at maybeCallback (C:coursesnode-jsshopping-cartnode_modulesmongodblibutils.js:293:21)
at executeOperation (C:coursesnode-jsshopping-cartnode_modulesmongodbliboperationsexecute_operation.js:12:38)
at Collection.insertOne (C:coursesnode-jsshopping-cartnode_modulesmongodblibcollection.js:157:57)
at NativeCollection.<computed> [as insertOne] (C:coursesnode-jsshopping-cartnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:243:33)
at Collection.doQueue (C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.js:128:23)
at C:coursesnode-jsshopping-cartnode_modulesmongooselibcollection.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
2
Answers
First thing i noticed is that your database connection is already established in the connectDB() function, so there’s no need to connect again before the loop. Secondly, the save() method returns a promise, but your function is not an asynchronous one. Try this instead:
That should handle the promise returned
https://www.mongodb.com/languages/mern-stack-tutorial
Use this link to setup your MERN project & connecting MongoDB.