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

Why is bcrypt showing errors on TravisCI?

我正在 运行 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 (, , )';
    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 (, , )';
    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) return 正确的值但是 console.log(variables[1] ) return一个空对象。

我不明白哪里出了问题。

PS: 我刚做了点什么。我无意中将该文件中的 console.log(process.env.PASSWORD) 推到了 github 并将其与 travis 集成在一起,看哪,我做 console.log 的行显示 process.env.PASSWORD未定义。我已采取进一步措施导入 dotenv 或 dtenv/config 但它仍未定义。我该怎么办?

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

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