when i run this code and add a product to the database using thunder client it produces this error
i also whitelisted my ip and also added 0.0.0.0/0 to the mongodb atlas
PS D:\E - Commerce\backend> node index.js
Server Running on Port 4000
D:\E - Commerce\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186
const err = new MongooseError(message);
^
MongooseError: Operation `products.find()` buffering timed out after 10000ms
at Timeout.<anonymous> (D:\E - Commerce\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186:23)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
Node.js v20.11.0
PS D:\E - Commerce\backend>
This is my index.js file
const port = 4000;
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const jwt = require("jsonwebtoken");
const multer = require("multer");
const cors = require("cors");
const path = require("path");
const { time } = require("console");
app.use(express.json());
app.use(cors());
// DataBase connection with MangoDB
mongoose.connect(
"mongodb+srv://Tamil:password2488@cluster0.5nh2chn.mongodb.net/e-commerce"
);
// API Creation
app.get("/", (req, res) => {
res.send("Express App is Running");
});
// Image Storage Engine
const storage = multer.diskStorage({
destination: "./upload/images",
filename: (req, file, cb) => {
return cb(
null,
`${file.fieldname}_${Date.now()}${path.extname(file.originalname)}`
);
},
});
const upload = multer({ storage: storage });
// Creating upload Endpoints for images
app.use("/images", express.static("upload/images"));
app.post("/upload", upload.single("product"), (req, res) => {
res.json({
success: 1,
image_url: `http://localhost:${port}/images/${req.file.filename}`,
});
});
// Schema for Creating Products
const Product = mongoose.model("Product", {
id: {
type: Number,
required: true,
},
name: {
type: String,
required: true,
},
image: {
type: String,
required: true,
},
category: {
type: String,
required: true,
},
new_price: {
type: Number,
required: true,
},
old_price: {
type: Number,
required: true,
},
date: {
type: Date,
default: Date.now,
},
avilable: {
type: Boolean,
default: true,
},
});
app.post("/addproduct", async (req, res) => {
let products = await Product.find({});
let id;
if (products.length > 0) {
let last_product_array = products.slice(-1);
let last_product = last_product_array[0];
id = last_product.id + 1;
} else {
id = 1;
}
const product = new Product({
id: id,
name: req.body.name,
image: req.body.image,
category: req.body.category,
new_price: req.body.new_price,
old_price: req.body.old_price,
});
console.log(product);
await product.save();
console.log("Saved");
res.json({
success: true,
name: req.body.name,
});
});
// Creating API for deleting Products
app.post("/removeproduct", async (req, res) => {
await Product.findOneAndDelete({ id: req.body.id });
console.log("Removed");
res.json({
success: true,
name: req.body.name,
});
});
// Creating API for getting all products
app.get("/allproducts", async (req, res) => {
let products = await Product.find({});
console.log("All Products Fetched");
res.send(products);
});
app.listen(port, (error) => {
if (!error) {
console.log("Server Running on Port " + port);
} else {
console.log("Error :" + error);
}
});
using post method i added the product
http://localhost:4000/addproduct/
and JSON content is
{
“name”: “Product 40”,
“old_price”: 50,
“new_price”: 40,
“category”: “kid”,
“image”: “http://localhost:4000/images/product_1707626365144.png”
}
and in thunder client, in response tab it shows
Status: ERROR
Size: 0 B
Time: 0 ms
Connection was forcibly closed by a peer.
after running 10s.
also without adding the product or any other operations, the server stops running after 30s of time and produces this error.
PS D:\E - Commerce\backend> node index.js
Server Running on Port 4000
D:\E - Commerce\backend\node_modules\mongoose\lib\connection.js:829
err = new ServerSelectionError();
^
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
at _handleConnectionErrors (D:\E - Commerce\backend\node_modules\mongoose\lib\connection.js:829:11)
at NativeConnection.openUri (D:\E - Commerce\backend\node_modules\mongoose\lib\connection.js:804:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-a3zigto-shard-00-00.5nh2chn.mongodb.net:27017' => ServerDescription {
address: 'ac-a3zigto-shard-00-00.5nh2chn.mongodb.net:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1264626,
lastWriteDate: 0,
error: MongoNetworkError: connect ETIMEDOUT 34.93.17.249:27017
at connectionFailureError (D:\E - Commerce\backend\node_modules\mongodb\lib\cmap\connect.js:379:20)
at TLSSocket.<anonymous> (D:\E - Commerce\backend\node_modules\mongodb\lib\cmap\connect.js:285:22)
at Object.onceWrapper (node:events:633:26)
at TLSSocket.emit (node:events:518:28)
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ETIMEDOUT 34.93.17.249:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '34.93.17.249',
port: 27017
}
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
},
'ac-a3zigto-shard-00-01.5nh2chn.mongodb.net:27017' => ServerDescription {
address: 'ac-a3zigto-shard-00-01.5nh2chn.mongodb.net:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1264625,
lastWriteDate: 0,
error: MongoNetworkError: connect ETIMEDOUT 34.93.216.164:27017
at connectionFailureError (D:\E - Commerce\backend\node_modules\mongodb\lib\cmap\connect.js:379:20)
at TLSSocket.<anonymous> (D:\E - Commerce\backend\node_modules\mongodb\lib\cmap\connect.js:285:22)
at Object.onceWrapper (node:events:633:26)
at TLSSocket.emit (node:events:518:28)
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ETIMEDOUT 34.93.216.164:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '34.93.216.164',
port: 27017
}
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
},
'ac-a3zigto-shard-00-02.5nh2chn.mongodb.net:27017' => ServerDescription {
address: 'ac-a3zigto-shard-00-02.5nh2chn.mongodb.net:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1264655,
lastWriteDate: 0,
error: MongoNetworkError: connect ETIMEDOUT 35.244.56.252:27017
at connectionFailureError (D:\E - Commerce\backend\node_modules\mongodb\lib\cmap\connect.js:379:20)
at TLSSocket.<anonymous> (D:\E - Commerce\backend\node_modules\mongodb\lib\cmap\connect.js:285:22)
at Object.onceWrapper (node:events:633:26)
at TLSSocket.emit (node:events:518:28)
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ETIMEDOUT 35.244.56.252:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '35.244.56.252',
port: 27017
}
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-7gjnfs-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
Node.js v20.11.0
PS D:\E - Commerce\backend>
anyone know how to solve this issue