为什么bcrypt在TravisCI上显示错误?

时间:2019-01-17 16:46:55

标签: javascript travis-ci bcrypt

我正在TravisCI上运行测试,但此时,触发了npm测试,引发了此错误:

     > store-manager@1.0.0 test /home/travis/build/danoseun/Store-Manager
        > npm run createTables && nyc --reporter=html --reporter=text mocha ./server/tests/*.js --exit --compilers js:babel-core/register
        > store-manager@1.0.0 createTables /home/travis/build/danoseun/Store-Manager
        > babel-node -- ./server/db/dbTables
        /home/travis/build/danoseun/Store-Manager/node_modules/bcrypt/bcrypt.js:92
                throw new Error('data and salt arguments required');
                ^
        **Error: data and salt arguments required**
            at Object.hashSync (/home/travis/build/danoseun/Store-Manager/node_modules/bcrypt/bcrypt.js:92:15)
            at Object.<anonymous> (/home/travis/build/danoseun/Store-Manager/server/db/dbTables/seedAdmin/insertAdmin.js:6:28)
            at Module._compile (internal/modules/cjs/loader.js:721:30)
            at loader (/home/travis/build/danoseun/Store-Manager/node_modules/babel-register/lib/node.js:144:5)
            at Object.require.extensions.(anonymous function) [as .js] (/home/travis/build/danoseun/Store-Manager/node_modules/babel-register/lib/node.js:154:7)
            at Module.load (internal/modules/cjs/loader.js:620:32)
            at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
            at Function.Module._load (internal/modules/cjs/loader.js:552:3)
            at Module.require (internal/modules/cjs/loader.js:657:17)
            at require (internal/modules/cjs/helpers.js:22:18)
        npm ERR! code ELIFECYCLE
        npm ERR! errno 1
        npm ERR! store-manager@1.0.0 createTables: `babel-node -- ./server/db/dbTables`
        npm ERR! Exit status 1
        npm ERR! 
        npm ERR! Failed at the store-manager@1.0.0 createTables script.
        npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
        npm ERR! A complete log of this run can be found in:
        npm ERR!     /home/travis/.npm/_logs/2019-01-15T14_24_23_739Z-debug.log
        npm ERR! Test failed.  See above for more details.
        The command "npm test" exited with 1.
        cache.2
        store build cache
        0.00s2.35snothing changed, not updating cache
        Done. Your build exited with 1.

通过这种方式,insertAdmin.js文件如下所示:

    import bcrypt from 'bcrypt';
    import pool from '../../connection';



const sql = 'insert into users (email, password, role) values ($1, $2, $3)';
    const password = process.env.PASSWORD;
    const newPassword = bcrypt.hashSync(password, 10);
    const email = process.env.EMAIL;
    const variables = [email, newPassword, 'admin'];

    I also tried to restructure the file to use asynchronous hashing but it still didn't work.

    const sql = 'insert into users (email, password, role) values ($1, $2, $3)';
    const password = process.env.PASSWORD;

    async function value() {
         console.log('HERE', bcrypt.hash(password, 10))
         const hashPassword = await bcrypt.hash(password, 10);
        console.log('OYA', hashPassword);
         return hashPassword;
         }
        const email = process.env.EMAIL;
        const variables = [email, value(), 'admin'];
        console.log('NOW', variables[1]);

console.log(bcrypt.hash(password,10))和console.log(hashPassword)返回正确的值,但console.log(variables [1])返回一个空对象。

我不明白可能是什么问题。

PS:我刚刚做了一些事情。我无意中将该文件中的console.log(process.env.PASSWORD)推送到了github,并与travis和be整合,我在console.log所在的行中显示process.env.PASSWORD未定义。我已经采取了进一步的步骤来导入dotenv或dtenv / config,但仍未定义。我该怎么办?

2 个答案:

答案 0 :(得分:1)

process.env.PASSWORD可能是undefined,因为您忘记在Travis CI上配置环境变量。由于密码通常是机密的,因此您应该在存储库的设置页面中设置变量,这将对密码进行加密并将其保密。查看有关如何进行此操作的更多信息here

答案 1 :(得分:0)

要添加到建议的答案中,根据https://github.com/kelektiv/node.bcrypt.js/blob/master/bcrypt.js第92行,当bcrypt.hashSync方法的参数之一为null时,将引发bcrypt中的所需数据和盐参数错误。在Travis上解决此问题,在存储库的TravisCI上单击更多选项设置和环境变量,然后添加(使用添加按钮).env文件中指定的相应密钥。如果其他条件相同,则构建应会自动重新启动并通过。