<code>export async function getViews(id: string) {
try {
await client.connect();
const collection = client.db("views").collection("views");
const data = await collection.findOne({id});
if(!data) {
return 0;
}
return data.views;
}
finally {
await client.close();
}
}
</code>
<code>export async function getViews(id: string) {
try {
await client.connect();
const collection = client.db("views").collection("views");
const data = await collection.findOne({id});
if(!data) {
return 0;
}
return data.views;
}
finally {
await client.close();
}
}
</code>
export async function getViews(id: string) {
try {
await client.connect();
const collection = client.db("views").collection("views");
const data = await collection.findOne({id});
if(!data) {
return 0;
}
return data.views;
}
finally {
await client.close();
}
}
This is my code. When it runs, it should return the views number of the according document in the collection “views” of the database “views” but instead, it throws a huge number of errors
Here are a few:
<code>MongoExpiredSessionError: Cannot use a session that has ended
</code>
<code>MongoExpiredSessionError: Cannot use a session that has ended
</code>
MongoExpiredSessionError: Cannot use a session that has ended
<code>MongoTopologyClosedError: Topology is closed
</code>
<code>MongoTopologyClosedError: Topology is closed
</code>
MongoTopologyClosedError: Topology is closed
<code>TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ TypeError: Cannot read properties of undefined (reading 'close')
at async getViews (./src/app/posts.ts:71:9)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "486103747"
⨯ MongoTopologyClosedError: Topology is closed
at async getViews (./src/app/posts.ts:63:22)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "1105313916"
⨯ TypeError: Cannot read properties of undefined (reading 'close')
at async getViews (./src/app/posts.ts:71:9)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "486103747"
⨯ MongoTopologyClosedError: Topology is closed
at async getViews (./src/app/posts.ts:63:22)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "1105313916"
</code>
<code>TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ TypeError: Cannot read properties of undefined (reading 'close')
at async getViews (./src/app/posts.ts:71:9)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "486103747"
⨯ MongoTopologyClosedError: Topology is closed
at async getViews (./src/app/posts.ts:63:22)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "1105313916"
⨯ TypeError: Cannot read properties of undefined (reading 'close')
at async getViews (./src/app/posts.ts:71:9)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "486103747"
⨯ MongoTopologyClosedError: Topology is closed
at async getViews (./src/app/posts.ts:63:22)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "1105313916"
</code>
TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ unhandledRejection: TypeError: Cannot read properties of undefined (reading 'close')
at MongoClient.close (D:XXXXXnode_modulesmongodblibmongo_client.js:260:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async addView (webpack-internal:///(rsc)/./src/app/posts.ts:56:9)
⨯ TypeError: Cannot read properties of undefined (reading 'close')
at async getViews (./src/app/posts.ts:71:9)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "486103747"
⨯ MongoTopologyClosedError: Topology is closed
at async getViews (./src/app/posts.ts:63:22)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "1105313916"
⨯ TypeError: Cannot read properties of undefined (reading 'close')
at async getViews (./src/app/posts.ts:71:9)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "486103747"
⨯ MongoTopologyClosedError: Topology is closed
at async getViews (./src/app/posts.ts:63:22)
at async getPostDetails (./src/app/posts.ts:123:24)
at async Page (./src/app/blog/[id]/page.tsx:31:15)
digest: "1105313916"
BTW I’m using all this in Next.js app.
And everything is loaded in a Server Side rendered component.
I tried doing everything, I checked docs, everything seems correct, but it seems like the connecting to the client and closing the connection seems very hard.
Nothing worked
Please take a look at this and help please.
It’s for a blog website I’m making
5