简体   繁体   English

在 axios 中使用变量作为查询

[英]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.我正在尝试使用变量userEmail (分配给当前登录我的应用程序的用户的 email)作为对我的 api 的查询,因为我只想从我的 mongodb 搜索并返回与该特定 email 相关的文档。我我正在努力弄清楚如何将这个变量从我的 reactjs 应用程序传递到我的 api 并利用它,我什至不知道是否可能,所以任何帮助都会很棒。

I'm using axios to make my GET request.我正在使用 axios 发出我的 GET 请求。

api.js contains my api helper functions api.js 包含我的 api 辅助函数

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. dateApi.js 是 api surveyApiGet() 调用,目前我的查询只是匹配任何电子邮件的正则表达式,而不是仅过滤到当前用户。

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 Landing.js 是我调用我的 api 助手 function 的地方

surveyApiGet()
        .then(response => {
          // code
        })
        .catch(err => {
          err.message;
        });

For this, you can do a POST request to your Nodejs API and you can include the user's email with that request.为此,您可以向 Nodejs API 发出 POST 请求,并且可以在该请求中包含用户的 email。 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.然后通过Axios,可以将这段代码post到Nodejs API如下。

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.将此发送到 Nodejs 后,您可以通过请求的主体保存用户 email 的值,如下所示。 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.如果您有任何不清楚的地方,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM