Challenges with Filtering in MongoDB

I can't seem to figure out what's going wrong with my code. I've checked the documentation and it all appears to be correct. I've also gone through several posts and attempted various solutions.

Code:

router.get('/rank/:rank', async (req, res) => {
    const users = await loadUserCollection()
    const query = users.find({rank: req.params.rank}, {projection: {email: 0, password: 0}})
    res.json(query)
})

async function loadUserCollection() {
    const client = await mongodb.MongoClient.connect(mongoUri, {useNewUrlParser: true})
    return client.db('myFirstDatabase').collection('users')
}

ERROR:

[0] (node:39544) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
[0]     --> starting at object with constructor 'ReplSet'
[0]     |     property 's' -> object with constructor 'Object'
[0]     |     property 'coreTopology' -> object with constructor 'ReplSet'
[0]     |     ...
[0]     |     property 's' -> object with constructor 'Object'
[0]     --- property 'topology' closes the circle
[0]     at JSON.stringify (<anonymous>)
[0]     at stringify (C:\______________\node_modules\express\lib\response.js:1123:12)
[0]     at ServerResponse.json (C:\_________\node_modules\express\lib\response.js:260:14)      
[0]     at C:\____________\routes\api\users.js:19:9
[0]     at processTicksAndRejections (internal/process/task_queues.js:95:5)

Answer №1

Experiment with the following code: const query = await users.find({level: req.params.level}, {projection: {username: 1, age: 1}})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The npm installation process gets stuck

Here is the content of my package.json: { "name": "my-example-app", "version": "0.1.0", "dependencies": { "request": "*", "nano": "3.3.x", "async": "~0.2" } } After trying to run npm install in the command prompt, I am experiencing a hang during ...

Guide to adding server action listeners in a Vue.js application

I am currently working on developing a straightforward chat application using vuejs and socketio. My goal is to send messages from one user to all other users in the chat. Below is the code I have implemented on the server side to achieve this: io.on(&apo ...

Using websockets in a React client application

Attempting to establish a connection with a backend layer running on localhost, here is the provided source code: const { createServer } = require("http"); const cors = require("cors"); const photos = require("./photos"); const app = require("express")( ...

Different Vue sorting libraries to choose from

Currently, I am in the process of sorting data on the frontend while utilizing VueJS within my application. Instead of developing the logic for sorting from scratch, I am interested in knowing if there are any libraries available that can assist with this ...

Using JavaScript and the Firefox browser, learn how to easily highlight an element with Selenium-WebDriver

I am struggling with creating a valid function to highlight specific elements on a webpage. As a beginner in coding, I suspect that the issue may either be related to my environment setup or a lack of knowledge about JavaScript/Selenium features. I am wri ...

Executing a callback in Node.js after a loop is complete

As a newcomer to Node, I have been facing difficulties with the asynchronous nature of the platform while attempting to append data in a for loop of API calls. function emotionsAPI(data, next){ for(let url in data) { if(data.hasOwnProperty(url ...

What could be causing Heroku to encounter issues while compiling the build of the project?

I'm experiencing a challenge in deploying my react application on Heroku. Every time I attempt to push to the master branch, an error message stating "failed to compile node.js" is displayed. Despite following the recommended steps to specify the node ...

How can I ensure that I am only retrieving the first object within a "for loop" in vuejs and returning its value without getting the rest?

Why am I only able to retrieve the value of the first object in my "for loop" and not all three values as intended? var app = new Vue({ el: '#app', data: { myObj: [ {name: 'Hello', age: 10}, {name: ...

Execute a Cron Job every half an hour following the onCreate event in Firestore

Looking to set up a cron job or scheduler that runs every 30 minutes following an onCreate event in Firestore. The goal is to execute a cloud function that retrieves documents created within the last 30 minutes, validates them against a JSON schema, and ...

I'm having trouble getting my application to output to the console. What could be the issue?

In my cr-route.js file, I have a function that ensures the user is authenticated before displaying their name. module.exports = function (app, passport) { // ===================================== // HOME PAGE (with login links) ======== // == ...

What steps should I take to modify this Vue.js function?

<script lang="js"> import { ref } from 'vue'; export default { setup(){ const dateInput = ref(new Date()); function handleDateSelection(payload : Date): void { console.log(payload); } return ...

What is the correct way to integrate a HTML/CSS/JS theme into a Vue project effectively?

As a newcomer, I recently acquired a bootstrap theme that comes with HTML, CSS, and JavaScript files. My goal now is to integrate this theme into Vue in order to make it fully functional. The challenge I am facing is how to successfully incorporate the the ...

Update content in the collection

I'm struggling to find a way to update an item in my array. What I aim to achieve is to remove the item before adding it to the list if it already exists. I want to avoid iterating through all items to find the one that needs to be deleted. I attemp ...

What is the method to ensure a particular version of a dependency is utilized?

Is there a secure way to ensure that all packages use the same version of an npm dependency? The problem is one package is using an outdated version, and I want to update it (along with all other packages) to the latest version. ...

npm request user authentication while operating within a corporate proxy environment

When utilizing npm within a corporate proxy environment, it is necessary to include specific configurations in the .npmrc file located in the user's home directory. proxy = http://<username>:<pass>@<proxy_host>:<proxy_port>/ r ...

Removing an object within another object in MongoDB (excluding arrays): A guide to clean up your data efficiently

Imagine this data structure: { "_id" : ObjectId("52af7c1497bcaf410e000002"), "created_at" : ISODate("2013-12-16T22:17:56.219Z"), "name" : "Hot", "subcategories" : { "Loco" : { "subcategory" : "Loco", "id ...

Why doesn't updating Vuex state cause a re-render of the component?

I implemented a navigation bar that changes its display based on authentication: <template> <div id="routing"> <b-navbar variant="light" type="light"> <b-navbar-brand> <img id="drage" src="../assets/icon.svg" ...

Encountered a problem while attempting to post in Angular, receiving an error message stating "net::ERR

I recently started learning Nodejs. I've created an API on a local server using Mysql and I'm working on the frontend with Angular, while using Nodejs and Express as the backend. However, I'm facing an issue where my Angular app cannot conne ...

Troubleshooting Vue.js and Laravel: Having trouble loading new image files, while the older ones load just fine

Currently, I am working on a project that involves Vue.js and Laravel. In this setup, Vue is located inside the resources/js directory of the Laravel project. My current issue revolves around loading an image from the resources/js/assets directory. While ...

Feeling uncertain about Node, NPM, Bower, and how to set them up for Bootstrap?

Looking to expand my knowledge in web development, I have a solid understanding of HTML, JS, CSS, and server-side programming. However, the concepts of Nodejs, npm, and Bower are still unclear to me. In order to begin a new project, I created a designated ...