node.js回调函数

问题描述 投票:0回答:1

我想通过“ 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($1, $2, $3, $4, $5, $6, $7, $8)';
    var companyInfoSQL ='update companyinfo set address = $1, company_number = $2, company_phonenumber = $3, Business_license_number = $4 where company_code = $5';
    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($1, $2, $3)';
    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);
        }
    });
}
node.js callback cryptojs
1个回答
0
投票

我的建议是将其转换为Promise,然后可以使用Promise.all()等待多个异步操作完成。实际上,我想将所有这些回调都转换为Promise并清理很多代码,但这是对Promise的最小转换和一些附加错误处理的修改:

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($1, $2, $3, $4, $5, $6, $7, $8)';
    var companyInfoSQL ='update companyinfo set address = $1, company_number = $2, company_phonenumber = $3, Business_license_number = $4 where company_code = $5';
    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($1, $2, $3)';
    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()命令都应具有回调和错误处理,并且您似乎没有正确地对其中的一些进行排序,以等待第一个完成,然后再运行下一个。但是,这些是单独的问题。

© www.soinside.com 2019 - 2024. All rights reserved.