The code isn't functioning as desired, as I'm trying to retrieve data using a specific filter

I have made a request to the MongoDB database to retrieve some data based on email. My client-side code is functioning correctly. Here's the code snippet:

useEffect(()=>{
    
    const email = user?.email;
    
    // console.log(email)
    
    const url = `http://localhost:5000/services?email=${email}`;
    
    console.log(url)
    
    fetch(url)
    
    .then(res => res.json())
    
    .then(data => setItems(data))
},[user])

The console.log is displaying the expected data.

However, the issue arises on the backend. I attempted to use console.log to retrieve the email and other information, but no output is being displayed—no errors either. Below is the API code that I implemented using Express.js:

app.get('/services',async(req,res)=>{
            
            const email = req.query.email;
            
            console.log(email)
            
            const query = {email:email};
            
            const cursor =  serviceCollection.find(query);
            
            const item = await cursor.toArray();
            
            res.send(item);
        });

Answer â„–1

Your code seems to be working fine without any issues so far.

Have you attempted calling your GET request directly from the browser?

You may want to inspect the data coming into the endpoint (/services) by checking on req and res.

Additionally, consider adding the request options in your fetch method like this:


var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};

const email = user?.email;
    
const url = `http://localhost:5000/services?email=${email}`;

fetch(url , requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

I recommend using Postman to make calls to that endpoint as it can simplify API debugging with a 3rd party tool like Postman.

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

Using Javascript to determine the frequency of a JSON array with ESLINT version 6

I'm struggling to create an algorithm that counts the occurrences while adhering to ESLint's strict standards in JavaScript. Here is my input table: [ { "id": 2, "name": "Health", "color": "0190fe" }, { "id": 3, "name": ...

Enhance your coding experience with Firebase Autocomplete on VScode

Recently, I installed VScode along with the necessary packages for JavaScript development. As I started writing code involving Firebase, I noticed that the autocomplete feature, which worked perfectly fine in Xcode, was not functioning in VScode. How can I ...

turn off event listener in vue

I am trying to figure out how to remove the event listener in this scenario. Since I am calling a method from within the event listener function, I need to use ES6 syntax and can't use named functions. How can I go about removing the event listener? ...

Interested in uploading numerous images using express-fileupload

I am looking to improve my image upload functionality by allowing multiple images to be uploaded in a single input field without limiting the number of uploads. Below is the current code snippet that handles individual image uploads: router.post('/a ...

Using an action code to retrieve the current user from firebase: A step-by-step guide

I am in the process of designing 2 registration pages for users. The initial page prompts the user to input their email address only. After they submit this information, the following code is executed: await createUserWithEmailAndPassword(auth, email.value ...

Is it possible that socke.io is having trouble connecting to flashsocket?

After upgrading from socket.io 1.3.3, I made sure socket.io connected to flash socket. socketObj = io('http://localhost:9090', {'transports' : ['flashsocket'],'reconnection delay': 20}); Console messages appear as ...

The expected type 'Control.Monad.Trans.Reader.ReaderT MongoContext IO a0' did not match the actual type 'IO ()'

I need assistance with printing a dot graph extracted from mongoDB and converting it into an image. run = do docs <- timeFilter -- utilizing the function to retrieve [Document] data from mongoDB let dot = onlyDot docs -- excluding unnecessar ...

Problem with roles assigned through reactions on Discord

I've been working on a discord bot reaction roles command and everything seems to be going smoothly, except for one issue that I'm facing. After booting up the bot and running the command to create the embed, everything works fine. However, when ...

What is the method for fetching the value from checkbox buttons in Bootstrap 4?

I am currently utilizing Bootstrap's button plugin for my buttons, specifically with 6 checkbox buttons. I am in need of a method to extract the value of each checked button in order to perform some calculations. However, I am struggling to find a sol ...

Knex - Querying multiple tables with sorting capabilities

Can you help me with translating this code snippet to knex.js? SELECT id, Sum(x.kills) AS total FROM (SELECT id, kills FROM bedwars_player_solo UNION ALL SELECT id, kills FR ...

The jCapSLide Jquery Plugin is experiencing compatibility issues in Chrome browser

While this JQuery plugin works perfectly in Internet Explorer and Firefox, it seems to be malfunctioning in Chrome. The plugin is not being recognized at all by Chrome, and the captions are appearing below the image instead of on top with a sliding effect. ...

Seeking assistance with producing results

Is there someone who can provide an answer? What will be the output of the code snippet below when logged to the console and why? (function(){ var a = b = 3; })(); console.log("Is 'a' defined? " + (typeof a !== 'u ...

Finding records in MongoDB using PHP where one timestamp field is older than another timestamp field based on a PHP script

Is there a way to retrieve an object from a MongoDB collection where a specific field1 (timestamp or date) is older/newer than another specific field2 (timestamp or date)? Consider the following example object: // Sample Object { name: 'test' ...

Numerous buttons available for inputting into individual text boxes all on one page

<html> <body> <script> function enterNumber(num) { var txt=document.getElementById("output").value; txt=txt + num; document.getElementById("output").value=txt; } </script> Select numbers: <br> <input type="button" val ...

What is the method for transferring form data to a different page?

Currently utilizing AngularJS version 1.5.6 and looking for guidance on properly passing form data using $location.path. Below is my code snippet for Page A: <form> ... <button type="submit" ng-click="submit(formData)"> ...

I have a requirement to conduct a test for my Spring batch application that involves fetching data from a MongoDB database

As a newcomer to testing, I've developed a Spring Batch job using mongodb and gradle. I need assistance starting with either a junit test or integration test to verify the correctness of my project. Despite searching through Stack, I didn't find ...

Leveraging Backbone.js without using client-side JavaScript

Exploring the idea of using Backbone.js and node.js to develop a compact web application. The concept of sharing code between the client and server is quite appealing. The challenge arises when considering how users without JavaScript-enabled browsers (in ...

Executing search bar capability through the use of AJAX and HTTP requests in JavaScript

I am currently working on implementing a search feature that will locate data based on the user's query. Specifically, I want to create a search bar in my HTML that can search for book titles stored in my database accessible through GET requests. Alth ...

Choose either the final choice in the dropdown menu or determine the total number of options available in the dropdown menu when using UI Vision, RPA, Kantu, Selenium

My challenge involves a drop-down menu with changing options. I am looking to dynamically select the last item in this menu, without knowing the total number of items present. I am seeking advice on using xpath for this feature. Specifically, I am using u ...

Smooth scroll animation without the use of jQuery

I am experimenting with creating an animated "scroll to top" functionality without relying on jQuery. Typically, in jQuery, I would implement this code: $('#go-to-top').click(function(){ $('html,body').animate({ scrollTop: 0 }, ...