node.js 回调函数
node.js callback Funtion
我想通过 "SHA512crypto" 回调函数接收结果值,并将其用作 client.query 中的值。
但是无论我多么努力,我都只能得到未定义的值。请no.js初学者给出解决方案
var SHA512crypto = function (KeyValue, callback) {
crypto.randomBytes(64, (err, buf) => {
crypto.pbkdf2(KeyValue, buf.toString('base64'), 97325, 64, 'sha512', (err, key) => {
//console.log(key.toString('base64'));
callback(err, key.toString('base64'));
return;
});
});
}
// Socket SignUp Data
var socketSignUp = function (SignUpData, callback) {
var SignUpSQL = 'insert into userinfo values(, , , , , , , )';
var companyInfoSQL ='update companyinfo set address = , company_number = , company_phonenumber = , Business_license_number = where company_code = ';
var companyInfoValues = [SignUpData.address, SignUpData.admin_company_number, SignUpData.admin_phone_number, SignUpData.businessLicense, SignUpData.company_code];
var LoginInfoSQL = 'insert into logininfo(email, status, company_code) values(, , )';
var LoginInfoValues = [SignUpData.email, "false", SignUpData.company_code];
var TimeStampNumSQL = 'SELECT timestamp FROM userinfo ORDER BY timestamp DESC LIMIT 1';
client.query(TimeStampNumSQL, (err, res) => {
if (res) {
var SignUpValues = [res.rows[0].timestamp + 1, SignUpData.admin, SHA512crypto(SignUpData.email), SHA512crypto(SignUpData.password), SignUpData.name, SignUpData.company_code, SignUpData.company_number, SignUpData.phone_number];
client.query(SignUpSQL, SignUpValues);
if (SignUpData.admin == "admin") {
client.query(companyInfoSQL, companyInfoValues);
}
client.query(LoginInfoSQL, LoginInfoValues);
}
});
}
我的建议是转换为 Promise,然后您可以使用 Promise.all()
等待多个异步操作完成。实际上,我想将所有这些回调转换为 promises 并大量清理代码,但这里有一个修改,对 promises 的转换最少,并添加了一些错误处理:
var SHA512crypto = function (KeyValue) {
return new Promise(function(resolve, reject) {
crypto.randomBytes(64, (err, buf) => {
if (err) return reject(err);
crypto.pbkdf2(KeyValue, buf.toString('base64'), 97325, 64, 'sha512', (err, key) => {
//console.log(key.toString('base64'));
if (err) return reject(err);
resolve(key.toString('base64'))
});
});
});
}
// Socket SignUp Data
var socketSignUp = function (SignUpData, callback) {
var SignUpSQL = 'insert into userinfo values(, , , , , , , )';
var companyInfoSQL ='update companyinfo set address = , company_number = , company_phonenumber = , Business_license_number = where company_code = ';
var companyInfoValues = [SignUpData.address, SignUpData.admin_company_number, SignUpData.admin_phone_number, SignUpData.businessLicense, SignUpData.company_code];
var LoginInfoSQL = 'insert into logininfo(email, status, company_code) values(, , )';
var LoginInfoValues = [SignUpData.email, "false", SignUpData.company_code];
var TimeStampNumSQL = 'SELECT timestamp FROM userinfo ORDER BY timestamp DESC LIMIT 1';
client.query(TimeStampNumSQL, (err, res) => {
if (err) {
callback(err);
return;
}
if (res) {
Promise.all([SHA512crypto(SignUpData.email), SHA512crypto(SignUpData.password)])
.then(([emailCrypto, passwordCrypto]) => {
var SignUpValues = [res.rows[0].timestamp + 1, SignUpData.admin, emailCrypto, passwordCrypto, SignUpData.name, SignUpData.company_code, SignUpData.company_number, SignUpData.phone_number];
client.query(SignUpSQL, SignUpValues);
if (SignUpData.admin == "admin") {
client.query(companyInfoSQL, companyInfoValues);
}
client.query(LoginInfoSQL, LoginInfoValues);
}).catch(callback);
}
});
}
你的所有 client.query()
命令似乎都应该有回调和错误处理,而且你似乎没有正确排序其中的一些命令以等待第一个命令在 运行下一个。但是,这些是与您提出的问题不同的问题。
我想通过 "SHA512crypto" 回调函数接收结果值,并将其用作 client.query 中的值。 但是无论我多么努力,我都只能得到未定义的值。请no.js初学者给出解决方案
var SHA512crypto = function (KeyValue, callback) {
crypto.randomBytes(64, (err, buf) => {
crypto.pbkdf2(KeyValue, buf.toString('base64'), 97325, 64, 'sha512', (err, key) => {
//console.log(key.toString('base64'));
callback(err, key.toString('base64'));
return;
});
});
}
// Socket SignUp Data
var socketSignUp = function (SignUpData, callback) {
var SignUpSQL = 'insert into userinfo values(, , , , , , , )';
var companyInfoSQL ='update companyinfo set address = , company_number = , company_phonenumber = , Business_license_number = where company_code = ';
var companyInfoValues = [SignUpData.address, SignUpData.admin_company_number, SignUpData.admin_phone_number, SignUpData.businessLicense, SignUpData.company_code];
var LoginInfoSQL = 'insert into logininfo(email, status, company_code) values(, , )';
var LoginInfoValues = [SignUpData.email, "false", SignUpData.company_code];
var TimeStampNumSQL = 'SELECT timestamp FROM userinfo ORDER BY timestamp DESC LIMIT 1';
client.query(TimeStampNumSQL, (err, res) => {
if (res) {
var SignUpValues = [res.rows[0].timestamp + 1, SignUpData.admin, SHA512crypto(SignUpData.email), SHA512crypto(SignUpData.password), SignUpData.name, SignUpData.company_code, SignUpData.company_number, SignUpData.phone_number];
client.query(SignUpSQL, SignUpValues);
if (SignUpData.admin == "admin") {
client.query(companyInfoSQL, companyInfoValues);
}
client.query(LoginInfoSQL, LoginInfoValues);
}
});
}
我的建议是转换为 Promise,然后您可以使用 Promise.all()
等待多个异步操作完成。实际上,我想将所有这些回调转换为 promises 并大量清理代码,但这里有一个修改,对 promises 的转换最少,并添加了一些错误处理:
var SHA512crypto = function (KeyValue) {
return new Promise(function(resolve, reject) {
crypto.randomBytes(64, (err, buf) => {
if (err) return reject(err);
crypto.pbkdf2(KeyValue, buf.toString('base64'), 97325, 64, 'sha512', (err, key) => {
//console.log(key.toString('base64'));
if (err) return reject(err);
resolve(key.toString('base64'))
});
});
});
}
// Socket SignUp Data
var socketSignUp = function (SignUpData, callback) {
var SignUpSQL = 'insert into userinfo values(, , , , , , , )';
var companyInfoSQL ='update companyinfo set address = , company_number = , company_phonenumber = , Business_license_number = where company_code = ';
var companyInfoValues = [SignUpData.address, SignUpData.admin_company_number, SignUpData.admin_phone_number, SignUpData.businessLicense, SignUpData.company_code];
var LoginInfoSQL = 'insert into logininfo(email, status, company_code) values(, , )';
var LoginInfoValues = [SignUpData.email, "false", SignUpData.company_code];
var TimeStampNumSQL = 'SELECT timestamp FROM userinfo ORDER BY timestamp DESC LIMIT 1';
client.query(TimeStampNumSQL, (err, res) => {
if (err) {
callback(err);
return;
}
if (res) {
Promise.all([SHA512crypto(SignUpData.email), SHA512crypto(SignUpData.password)])
.then(([emailCrypto, passwordCrypto]) => {
var SignUpValues = [res.rows[0].timestamp + 1, SignUpData.admin, emailCrypto, passwordCrypto, SignUpData.name, SignUpData.company_code, SignUpData.company_number, SignUpData.phone_number];
client.query(SignUpSQL, SignUpValues);
if (SignUpData.admin == "admin") {
client.query(companyInfoSQL, companyInfoValues);
}
client.query(LoginInfoSQL, LoginInfoValues);
}).catch(callback);
}
});
}
你的所有 client.query()
命令似乎都应该有回调和错误处理,而且你似乎没有正确排序其中的一些命令以等待第一个命令在 运行下一个。但是,这些是与您提出的问题不同的问题。