Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - 在节点会话中创建访问级别作为功能(Create Access Level as function in Node Session)

I'm trying to create access levels in Node.js using Session.(我正在尝试使用Session在Node.js中创建访问级别。)

So for example, if the username is "test" it can view a certain page.(因此,例如,如果用户名是“ test”,则可以查看特定页面。) If the username is "admin" it cannot view that same page.(如果用户名是“ admin”,则无法查看该页面。) The username is stored in MySQL table "accounts".(用户名存储在MySQL表“ accounts”中。) The table has the column attribute "username".(该表具有列属性“用户名”。) Been trying to try different things but I can't seem to get it to work.(一直试图尝试不同的东西,但我似乎无法使它正常工作。) Here's my JS code:(这是我的JS代码:) app.get('/ViewPage', function(request, response) { var connection = request.app.get('pool'); if (request.session.loggedin) { var username; connection.query('SELECT username FROM accounts WHERE username = test', function (error, results, fields) { if (username == "test") { // do the command here } else if (username == "admin"){ response.redirect('/'); } }); } else { response.redirect('/'); } }); Edit: I updated my code as follows but it keeps looping to the "Admin cannot view this page" else if block.(编辑:我更新了我的代码,如下所示,但它一直循环到“管理员无法查看此页面”,否则将被阻止。) app.get('/Create_Award', function (request, response) { var connection = request.app.get('pool'); if (request.session.loggedin) { connection.query('SELECT username FROM accounts', function (error, results, fields) { { account: results[0] }; if (error) { console.log(error); } else { if (results[0].username === "test") { connection.query('SELECT accounts.id, accounts.username, awardType.id as awardTypeId, awardType.title FROM accounts JOIN awardType WHERE username = ?', [request.session.username], function(error, results, fields) { response.render('Create_Award.html', { account: results[0], data: results }); console.log('Test account.'); }); //2nd connection } else if (results[0].username === "admin") { response.redirect('/'); console.log('Admin cannot view this page.'); } else { response.redirect('/'); } } }); //1st connection } else { response.redirect('/'); } });   ask by heheh124 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are never populating your var username as it will always will be undefined and your code will go to else block(您永远不会填充您的var username ,因为它将始终是undefined并且您的代码将进入else块)

app.get('/ViewPage', function (request, response) { var connection = request.app.get('pool'); if (request.session.loggedin) { connection.query('SELECT username FROM accounts WHERE username = test', function (error, results, fields) { if (error) { console.log(error); } else { if (results[0].username === "test") { response.redirect('/myCustom'); } else if (results[0].username === "admin") { response.redirect('/'); } else { response.redirect('/'); } } }); } else { response.redirect('/'); } }); Update your code and it will work(更新您的代码,它将起作用) Edit - From what I can see understand from your updated code, the only thing incorrect is how you are using res.render .(编辑 -从更新的代码中我们可以了解到,唯一不正确的是您如何使用res.render 。) have a look here , it takes a callback function(在这里看看,它需要一个callback function) Try this in your code and let me know if it works(在您的代码中尝试一下,让我知道它是否有效) response.render('Create_Award.html', { account: results[0], data: results },(err,html)=>{ res.send(html) });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...