为了账号安全,请及时绑定邮箱和手机立即绑定

在 Nodejs 中创建局部变量的全局副本

在 Nodejs 中创建局部变量的全局副本

鸿蒙传说 2023-09-28 10:22:02
我已经用 Express 提取了用户名、密码和另一个密码,如下所示: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;});然后我想用我创建的新变量做一些事情。我如何在此功能之外使用它们?
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

您可以将它们设置为全局或将它们传递给下一个函数


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;

});

或者传递它们:


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) {

    ...

}


查看完整回答
反对 回复 2023-09-28
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信