将变量发送到布局 [英] Send variables to layout
问题描述
我正在学习如何使用Node.js来构建Web应用程序并进行表达,所以我现在还真不行.
I'm learning how to build web applications using Node.js and express, so I'm really noob yet.
所以,我在这里有一些问题.我正在构建登录页面,从数据库(在mysql中)获得的所有信息都将显示在单个页面中.
So, I have some questions here. I'm building a landing page, and all the informations that I'm getting from my Database (in mysql) will appear in a single page.
我正在将值从数据库发送到Jade内置的布局中.
I'm sending values from my database, to my layout, built in Jade.
我创建了多个函数来获取特定数据,这里是一个示例:
And I created multiple functions to get specific data, here an example:
function getUser(username, userId, callback) {
connection.query('SELECT * FROM users WHERE user_id = ?', userId, function(err, result) {
if (err)
callback(err, null);
else
var callBackString = {};
callBackString.value1 = result[0].user_email;
callBackString.value2 = result[0].user_name;
callback(null, callBackString);
});
}
当用户尝试登录时,我检查用户是否存在以更改布局并将一些重要值发送给布局:
When the user tries to login I check if the user exists to change the layout and send to the layout some important values:
router.post('/login', function(req, res) {
connection.query('SELECT user_id FROM users WHERE user_email = ? AND user_password = ?', [req.body.login, req.body.password], function(err, results) {
if (err) throw err;
if (results[0]) {
userId = results[0].user_id;
getUser("username", userId, function(err, data) {
if (err) {
console.log("ERROR : ", err);
} else {
res.render('logged_in', {
email: data.value1,
username: data.value2,
});
res.end();
}
});
} else {
res.render('index', {
validation: "failed"
});
}
});
});
我在这里只调用一个函数(getUser()),当我调用此函数时,布局会更改,并且会发送一些值.
I'm only calling one function here (getUser()), and when I call this function, the layout changes, and I send some values.
但是现在我想创建一个名为getPosts()的新函数,以从另一个表中获取信息,并将其也发送到布局中,就像我调用函数getUser()时所做的一样
But now I would like to create a new function called getPosts(), to get informations from a different table, and send it to the layout too, like I did when i called the function getUser()
我试图做这样的事情,但是我没有成功,当我调用范围之外的变量时,我总是变得未定义".
I tried to do something like this but I had no success, when I call the variables outside the scope I keep getting "undefined".
router.post('/login', function(req, res) {
connection.query('SELECT user_id FROM users WHERE user_email = ? AND user_password = ?', [req.body.login, req.body.password], function(err, results) {
if (err) throw err;
if (results[0]) {
userId = results[0].user_id;
getUser("username", userId, function(err, data) {
if (err) {
console.log("ERROR : ", err);
} else {
email = data.value1;
username = data.value2;
}
});
getPosts("posts", 1, function(err, data) {
if (err) {
console.log("ERROR : ", err);
} else {
postName = data.value1;
postText = data.value2;
}
});
res.render('logged_in', {
email: email,
username: username,
pstname: postName,
psttxt: postText
});
res.end();
} else {
res.render('index', {
validation: "failed"
});
}
});
});
我需要对我的代码进行哪些更改?谢谢.
What do I need to change on my code? Thank you.
推荐答案
您应该在 node.js
中阅读有关异步
的信息,因此,如果在下面更改代码,可能会工作:
You should read about asynchronization
in node.js
so if you change your code as bellow it may work:
router.post('/login', function(req, res) {
connection.query('SELECT user_id FROM users WHERE user_email = ? AND user_password = ?', [req.body.login, req.body.password], function(err, results) {
if (err) throw err;
if (results[0]) {
userId = results[0].user_id;
getUser("username", userId, function(err, data) {
if (err) {
console.log("ERROR : ", err);
} else {
email = data.value1;
username = data.value2;
getPosts("posts", 1, function(err, data) {
if (err) {
console.log("ERROR : ", err);
} else {
postName = data.value1;
postText = data.value2;
res.render('logged_in', {
email: email,
username: username,
pstname: postName,
psttxt: postText
}
});
}
});
} else {
res.render('index', {
validation: "failed"
});
}
});
});
这篇关于将变量发送到布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!