简体   繁体   English

在 Nodejs 中制作局部变量的全局副本

[英]Make global copy of local variables in Nodejs

I have extracted a username, password and another password with express like this:我已经提取了一个用户名、密码和另一个密码,如下所示:

var urlencodedParser = bodyparser.urlencoded({extended: false});
app.post('/', urlencodedParser, function(req, res) {
    console.log(req.body);
    var un = req.body.username;
    var pw1 = req.body.pwd1;
    var pw2 = req.body.pwd2;
});

I then want to do something with the new variables I have created.然后我想对我创建的新变量做一些事情。 How can I use them outside of this function?我如何在这个 function 之外使用它们?

You can either make them global or pass them to the next function您可以将它们设为全局或将它们传递给下一个 function

var un, pw1, pw2
var urlencodedParser = bodyparser.urlencoded({extended: false});
app.post('/', urlencodedParser, function(req, res) {
    console.log(req.body);
    un = req.body.username;
    pw1 = req.body.pwd1;
    pw2 = req.body.pwd2;
});

Or passing them on:或传递它们:

var urlencodedParser = bodyparser.urlencoded({extended: false});
app.post('/', urlencodedParser, function(req, res) {
    console.log(req.body);
    doSomething(req.body.username, req.body.pw1, req.body.pw2);
});

function doSomething(un, pw1, pw2) {
    ...
}

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

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