简体   繁体   English

Nodejs Mysql:在另一条路由中访问变量

[英]Nodejs Mysql: Accessing variable in another route

I'm building a simple e-wallet system that accepts user's phone number and query's the user's wallet from database and displays balance; 我正在构建一个简单的电子钱包系统,该系统可以接收用户的电话号码并从数据库中查询用户的钱包并显示余额; I built a form that receives a phone number and uses the number to query the database for user id and uses that id to query his/her e-payment wallet id but i can't pass the wallet to a get request to render the wallet balance and username 我建立了一个接收电话号码的表格,并使用该号码查询数据库中的用户ID,并使用该ID查询他/她的电子支付钱包ID,但是我无法将钱包传递给获取请求以呈现钱包余额和用户名

router.post('/',function (req, res, next) {
  var sql = `SELECT UsrID FROM users WHERE UsrPhone =  ${req.body}`;
  var query = connection.queryValue(sql, function (err, userid) {
    if(err) throw err;
    console.log('User ID: ' + userid);

    var sql = `SELECT * FROM wallet WHERE UserID = ${userid}`;
    var query = connection.queryRow(sql, function (err, wallet) {
      if(err) throw err;
      console.log('WalletID: ' + wallet.WalletID);
      //send walletID to get route
      res.redirect('/wallet/:id');
    });
  });
});

router.get('/wallet/:id', function (req, res, next) {
  var sql = `SELECT * FROM wallet WHERE WalletID = ${wallet.WalletID}`;
  var query = connection.queryRow(sql, function (err, wallet) {
    if(err) throw err;
    res.render('wallet', {title: 'Wallet', wallet});
  });
});

but it doesn't seem to work 但它似乎不起作用

You didn't pass the current WalletID to the redirect route. 您没有将当前的WalletID传递给重定向路由。 You should pass the id to redirect URL like this: 您应该传递id来重定向URL,如下所示:

res.redirect('/wallet/' + wallet.WalletID);

So, the wallet/:id can get the current WalletID and make the query base on that. 因此, wallet/:id可以获取当前的WalletID并在此基础上进行查询。

And you can run the query like this: 您可以像这样运行查询:

  var sql = `SELECT * FROM wallet WHERE WalletID = ${req.params.id}`;

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

相关问题 如何将 nodejs 请求路由到另一个 nodejs 应用程序? - How to route nodejs requests to another nodejs application? 将变量值从 index.js 访问到 nodejs 上的另一个 js 文件 - Accessing variable value from index.js to another js file on nodejs 如何在另一个函数(NodeJS mysql)的查询MySQL中使用“结果”变量(回调函数) - How to use “results” variable (callback function) inside query mysql in another function (Nodejs mysql) Hapi js从另一条路线进入一条路线 - Hapi js accessing one route from another 调用变量到在Node.js和Express中不起作用的其他路由 - Calling a variable to a different route not working in nodejs and express NodeJS - 访问另一个 Class 文件中的数组 - NodeJS - Accessing Array in Another Class File Vue:在路由保护(路由 beforeEach)中访问商店道具/变量? - Vue : Accessing store prop/variable in route guard (route beforeEach)? 返回变量 MySQL 的 NodeJS 问题 - NodeJS problem with returning variable MySQL Node.js如何在Node.js中以相同的路线重定向另一个页面 - Nodejs how to redirect another page in same route in nodejs 在JavaScript / nodejs中,访问变量与变量属性之间是否存在速度差异? - In JavaScript/nodejs, is there a speed difference between accessing a variable vs a property of a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM