Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda