如何从POST方法中带出变量

qvtsj1bj  于 2022-10-22  发布在  Go
关注(0)|答案(1)|浏览(113)

我想从登录表单中带来一个变量,当插入登录表单重定向到/PROFILE时,这个配置文件页面应该有来自页面中显示的登录的变量USERNAME,就像会话对当前用户帐户所做的那样...然而,尽管如此,我仍然无法使用所带来的变量呈现页面。还没有!
我该怎么做呢?
代码如下:

app.post('/login', function (req, res, next) {
    //console.log(req.body);
    const uri = "mongodb+srv://<USERNAME>:<PW>@<CLUSTER>.eapnyil.mongodb.net/?retryWrites=true&w=majority";
    const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
    const users = client.db("data").collection("users");
    users.findOne({email:req.body.email},function(err,data){
        if(data){

            if(data.password==req.body.password){
                //console.log("Logged In.");
                console.log(data.email);
                console.log(req.body.email);
                //res.send({"Success":"Success!"});
                res.redirect('/profile');
            }else{
                res.send({"Success":"Wrong password!"});
            }
        }else{
            res.send({"Success":"This Email Is not registered!"});
        }
    });
});

app.get('/profile', function (request, response) {

    console.log("Fetching profile");
    const uri = "mongodb+srv://<USERNAME>:<PW>@<CLUSTER>.eapnyil.mongodb.net/?retryWrites=true&w=majority";
    const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
    const users = client.db("data").collection("users");

    users.findOne({email:request.body.email},function(err,data){
        console.log(request.body.email);
        //response.render('pages/data', {users:username,users:email});
    })
});

就我所管理的而言,这最后一段代码‘USERS:USERNAME’似乎是未定义的。

...users.findOne({email:request.body.email},function(err,data){
        console.log(request.body.email);
        //response.render('pages/data', {users:username,users:email});
mrzz3bfm

mrzz3bfm1#

重定向可以有一些查询参数,因此无论您希望剖面路径中的值是什么
如下所述,只需在重定向的末尾传递

app.post('/login', function (req, res, next) {
    //console.log(req.body);
    const uri = "mongodb+srv://<USERNAME>:<PW>@<CLUSTER>.eapnyil.mongodb.net/?retryWrites=true&w=majority";
    const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
    const users = client.db("data").collection("users");
    users.findOne({email:req.body.email},function(err,data){
        if(data){

            if(data.password==req.body.password){
                //console.log("Logged In.");
                console.log(data.email);
                console.log(req.body.email);
                //res.send({"Success":"Success!"});
                >> let query = JSON.stringify(req.body.email); || (data) //whatever value u need to send to profile route
                res.redirect('/profile?email='+query);
            }else{
                res.send({"Success":"Wrong password!"});
            }
        }else{
            res.send({"Success":"This Email Is not registered!"});
        }
    });
});

app.get('/profile', function (request, response) {

    >> let email = JSON.parse(request.query.email)
    console.log("Fetching profile");
    const uri = "mongodb+srv://<USERNAME>:<PW>@<CLUSTER>.eapnyil.mongodb.net/?retryWrites=true&w=majority";
    const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
    const users = client.db("data").collection("users");

    users.findOne({email},function(err,data){
        console.log(request.body.email);
        //response.render('pages/data', {users:username,users:email});
    })
});
``

相关问题