V
V
Vladimir2019-11-17 22:15:09
JavaScript
Vladimir, 2019-11-17 22:15:09

Why doesn't bcrypt consider a number to be a numeric value?

Good night. Please tell me why, when exporting a variable, bcrypt gives an error that this is not a number, although there is a number in the variable. If I write just a number as a parameter, everything is ok.
Here's the export (packages.js)

const dotenv = require('dotenv')
const initializeDotenv =  dotenv.config({path: __dirname + '/.env'})
var path = require('path')
const express = require('express')
const session = require('express-session')
const util = require('util')
const mysql = require('mysql')
const fileStore = require('session-file-store')(session)
const passport = require('passport')
const localStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
//Complex hash for bcrypt
const saltRounds = process.env.COMPLAXITY

module.exports = {dotenv, path, express, session, util, mysql, fileStore, passport, localStrategy, bcrypt, saltRounds}

Here import and directly bcrypt
const { pool } = require('./mysql')
const { bcrypt,saltRounds,dotenv } = require('./packages')

//Create query
class NewQuery {

  constructor(type,table,inputs,values,select) {

    this.type = type
    this.table = table
    this.inputs = inputs
    this.values = values
    this.select = select

    this.newQuery = `${this.type} ${this.table} (${this.inputs}) VALUES(${this.values}`
    
    this.send = (password) => {

      return new Promise((resolve, reject) => {

        pool.query(this.select, (err,res) => {

          if (res.length < 1) {

            console.log(saltRounds)

            bcrypt.genSalt(saltRounds, (err, salt) => {
              if(err) console.error(`${err} - salt`)
          			bcrypt.hash(password, salt, (err, hash) => {

          				if(err) console.error(`${err} - hash`)

          				console.log(`${hash} ${salt} ${password} ${saltRounds}`)

                pool.query(`${this.newQuery},'${hash}')`, (err,res) => {
                	
                  	if(err) console.error(err)

                  	console.log(`Success: User created! ${this.newQuery} ${hash}`)

                })

              })
            })

          }

          else {

            console.log('Banned: User registered!');

          }

        })
      })

    }
  }
}

//Create object for query
class CreateQuery {
  constructor(type,table,inputs,values,select,password) {
    this.type = type
    this.table = table
    this.inputs = inputs
    this.values = values
    this.select = select
    this.password = password
  }
}

module.exports = { NewQuery, CreateQuery}

.env
#Project mode
NODE_ENV=production

#App port
PORT=3000

#Mysql

HOST=localhost

USER=root

PASSWORD=

DATABASE=test

SOCKET=

#Bcrypt

COMPLAXITY=10

#Secret for sessions

SECRET=$$999$$

Thanks in advance for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pashenka, 2019-11-19
@HistoryART

The fastest and preferred way to cast a string to a number is:
const saltRounds = +process.env.COMPLAXITY;

B
bpGusar, 2019-11-18
@bpGusar

environment variables are always strings, if you need a number you need to convert, for exampleconst saltRounds = Number(process.env.COMPLAXITY)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question