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

218
Visualizações
Using a variable as a query in axios

I'm attempting to use a variable userEmail (which is assigned the user's email who is currently logged into my app) as a query for my api as I only want to search and return documents from my mongodb that relate to that specific email. I'm struggling to figure out how to pass this variable from my reactjs app to my api and utilising it, I don't even know if it is possible so any help would be great.

I'm using axios to make my GET request.

api.js contains my api helper functions

export async function surveyApiGet () {

    let response = await axios.get('http://localhost:6000/'); // dateApi.js

    return response.data;
}

dateApi.js is the api surveyApiGet() calls and at the moment my query is just regex that matches any emails, rahter than filtering to just the current user.

let MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";
const express = require('express');
const app = express();

app.use(express.json());

app.get('/', function (req, res) {
    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        var dbo = db.db("manderleydb");
        var query = { userId : /^(.)/ };
        dbo.collection("manderleySurveyCompleted").find(query).sort({ date: 1}).limit(1)
            .toArray(function(err, result) {
                if (err) throw err;
                console.log(result);
                db.close();
                res.status(200).json(JSON.stringify(result));
        });
    });
});

app.listen(6000, () => {
    console.log('listening on port 6000');
});

Landing.js is where I make the call to my api helper function

surveyApiGet()
        .then(response => {
          // code
        })
        .catch(err => {
          err.message;
        });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For this, you can do a POST request to your Nodejs API and you can include the user's email with that request. To do so what you have to do is something like the following.

const usersEmail = "example@email.com"

Then through Axios, you can post this code to Nodejs API as follows.

const data = await axios.post("http://localhost:6000/", {
    usersEmail : usersEmail
});

After you send this to Nodejs you can hold the value of the user's email through the request's body as follows. Change your endpoint to post instead of get.

app.post("/", function (req, res) {
     const userEmail = req.body.userEmail;

     //Put your MongoDB query and the status code here.

})

Drop a comment if you have any unclear points.

about 4 years ago · Juan Pablo Isaza 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