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
Post a Comment