Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

359
Views
How to make query api for email that I can find data email wise for different users from mongoDB with expressJs , nodeJs

I am new in backend development with MongoDB, NodeJS and frontend development with React-JS. I wanted to make an API endpoint that find the data from MongoDB and filter by user email address when user logged in the system. My Login system is completely okay. But The problem is When I try to retrieve data from client side that time data is not showing(fetching) on the UI. It founds and empty array. I want to know what is the problem in my code.

Here is my API Code for backend:

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

Client Side Code:

useEffect(() => {
  const getMyProducts = async () => {
    const email = user?.email;
    console.log(email);
    const url = `http://localhost:5000/myproducts?email=${email}`;
    try {
      await fetch(url)
        .then(res => res.json())
        .then(data => setProducts(data))
    } catch (error) {
      console.log(error?.message)
    }
  }
  getMyProducts();
}, [user])
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I solved it, There was a mistake from me. When I added product in my inventory that time I did not save my email in database with that post. After save my email in database, now I am able to filter product by email and also displaying on UI.

    const handleProductPost = (event) => {
    event.preventDefault();
    const email = user.email; //before, I did not added this email when posting 
    const productName = event.target.productName.value;
    const image = event.target.image.value;
    const description = event.target.description.value;
    const price = event.target.price.value;
    const quantity = event.target.quantity.value;
    const supplierName = event.target.supplierName.value;

    const product = { email, productName, image, description, price, quantity, supplierName };

    fetch('https://back-pack-warehouse.herokuapp.com/product', {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body: JSON.stringify(product)
    })
        .then(res => res.json())
        .then(data => {
            if (data.insertedId) {
                toast('Product Added Successfully');
                event.target.reset();
            }
        });
}
over 4 years ago · Santiago Trujillo Report

0

The toArray function exists on the Cursor class from the Native MongoDB NodeJS driver. The find method in MongooseJS returns a Query object.

In this case, you can directly log or send your cursor using res.send(cursor).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!