1
我正在使用Koa模塊 - 節點js和mysql &遇到問題。 在註冊功能(內welcomeCtrl.js)無法在函數外部使用變量
var bcrypt = require('bcrypt');
module.exports = {
signup: function* (next) {
bcrypt.genSalt(10, function(err, salt) {
console.log(salt);
bcrypt.hash(password, salt, function(err, hash) {
// Store hash in your password DB.
console.log(hash); //no error
var hashedPassword = hash;
});
});
console.log(bcrypt.hash.hash); //gives error
//or
console.log(bcrypt.hash.hahedPassword); //gives error
queryString = "insert into user(email,name, phone, password, course_id, dpt_id) values('%s','%s','%s','%s','%s','%s')";
query = util.format(queryString, email, name, phone, hash, courseId, dptId);
results = yield databaseUtils.executeQuery(query);
this.redirect('/');
}
}
我打電話不同routes.js註冊後函數文件爲:
router.post('/signup', welcomeCtrl.signup);
這裏是databaseUtils.js的executeQuerry函數文件
var executeQuery = function(query, callback) {
pool.getConnection(function(err, connection) {
// Use the connection
connection.query(query, function(err, rows, fields) {
connection.release();
if(err) {
err.mysqlQuery = query;
logger.logError(err);
}
if(typeof callback == "function") {
callback(err, rows);
}
// Don't use the connection here, it has been returned to the pool.
});
});
};
module.exports = {
executeQuery: thunkify(executeQuery),
executePlainQuery: executeQuery
};
Is there any way to use hash variable outside the function so that it can be inserted in query?
我也試過,但它會拋出錯誤 - 意外,(結果)後的結果=產量databaseUtils.executeQuery(查詢); –
你實際上是否設置了使用'yield'?如果不是的話,你應該回到傳統的承諾。我會更新我的答案。 –
是'yield'全部設置好了,有時我用yield this.render來渲染一些html頁面,在這個例子中沒有。 –