Next.js Discord

Discord Forum

[--BEGINNER--]how to clone sensitive info like password?

Answered
Asian black bear posted this in #help-forum
Open in Discord
Avatar
Asian black bearOP
Im exporting
const { Client } = require('pg');

const db = new Client({
    user: 'user1',
    host: 'localhost',
    database: 'dbName',
    password: "p@ss",
    port: 1234,
});
export default db ;


when I imported it I found it very hard to store db on any other variable...
const client = db doesnt work , client is always null
but
const client = new Client({ ...db });
cloned it, i can see client.user in the consle correctly
but password is null ...
how to store db on client correctly ?

70 Replies

Avatar
@James4u by `password` which password did you mean?
Avatar
Asian black bearOP
the one in db > password: "p@ss",
Avatar
@Asian black bear the one in db > password: "p@ss",
Avatar
the password that you need to connect your DB?
Avatar
@James4u the password that you need to connect your DB?
Avatar
Asian black bearOP
yes!
Avatar
@Asian black bear yes!
Avatar
why would you need to clone that password?
Avatar
Asian black bearOP
i have console log, i logged user on db and client they both logged the write user name
when I tried the same with Password
db logged it correctly
client logged null
Avatar
@James4u why would you need to clone that password?
Avatar
Asian black bearOP
im just following a tutorial .. im not sure why they did that
Avatar
user would be public but passworld is a private one
Avatar
@Asian black bear im just following a tutorial .. im not sure why they did that
Avatar
don't just follow - unless you are not sure what you are gonna do
there is nothing I can assume you are gonna do with the password
Avatar
Asian black bearOP
Just to connect to the database
i tried client.connect(); it failed
password was not a string, after looking into it, it turned out to be null
Avatar
const db = new Client({
    user: 'user1',
    host: 'localhost',
    database: 'dbName',
    password: "p@ss",
    port: 1234,
});
do you really create db instance in this way or do you use env variables?
Avatar
@James4u const db = new Client({ user: 'user1', host: 'localhost', database: 'dbName', password: "p@ss", port: 1234, });
Avatar
Asian black bearOP
its exported from another file
import { Client } from 'pg';
const db = new Client({
    user: 'user1',
    host: 'localhost',
    database: 'dbName',
    password: "p@ss",
    port: 1234,
});
export default db ;


then imported in the new file
trying to clone it on const client then connect to the database
Avatar
@James4u do you have a string or do you import from env?
Avatar
Asian black bearOP
oh just a string
Avatar
@gin wdym by "clone"?
Avatar
Asian black bearOP
Avatar
cool
Avatar
Asian black bearOP
there is a file the seeds the database just to so we can have data to work with through out the tutorial, appearntly pg has been updated so the files are no longer working
Avatar
@Asian black bear im doing the tutorial for nextJS https://nextjs.org/learn/dashboard-app/setting-up-your-database
Avatar
I don't see anything there that says "clone the password". Instead I see a lot of using env files, that you don't have 🤔
Avatar
Asian black bearOP
original code was
after importing db
const client = await db.connect();
but that is no longer working as client is always undefined
Avatar
why is it always undefined?
Avatar
Asian black bearOP
so i kept trying to figure out why and what has changed so i can update my code
Avatar
do u get any errors
Avatar
@gin do u get any errors
Avatar
Asian black bearOP
just undefined when u call client.query(); I get error yes .. it saied somethin bout calling query from undefined
Avatar
@B33fb0n3 I don't see anything there that says "clone the password". Instead I see a lot of using env files, that you don't have 🤔
Avatar
Asian black bearOP
the file has const client = await db.connect(); that is no longer working, i assume it clones db but I can be wrong
Avatar
it doesnt clone anything, the word clone is not correct here
db.connect just returns an instance of the db after connect has finishede
u need to check the documentation of pg
there we go
check this
everything u need is on this page
and store everything like db logins and else in env files
everything that is dynamic and need to be protected
so in your case, u will have a object in a singleton that always returns the client after client.connect has finished
Avatar
@Asian black bear the file has `const client = await db.connect();` that is no longer working, i assume it clones db but I can be wrong
Avatar
That’s just creates a connection to the database with the preconfigured object (db) that contains the credentials
Avatar
@B33fb0n3 That’s just creates a connection to the database with the preconfigured object (db) that contains the credentials
Avatar
Asian black bearOP
yes i get that, but its no longer working for some reason
u are probably not calling client.connect in your exported db const
Avatar
@gin and store everything like db logins and else in env files
Avatar
Asian black bearOP
oh yea im trying to understand how do I import data from env file to the script later
Avatar
its everything automatic
just create a .env file
Avatar
Asian black bearOP
where ?
Avatar
in your root
Avatar
Asian black bearOP
ok
Avatar
check the tutorial
there should be a section that documents that i think
Avatar
@gin check the tutorial
Avatar
Asian black bearOP
ohh , im hosting postgress locally, im not using that Vercel's
its not practicle to let other ppl handle ur data anyway
Answer
Avatar
check this and u are good
also i recommend always checking the docs correctly otherwise u will waste a lot of time understanding things
Avatar
@Asian black bear It seems like you are new in the whole web developement thing. In developement you can't just start by watching a youtube video or ask other people and follow every step the guy in the video does or someone said to you. That works, until you get an error. So start by learning the basics and then one after another. Right now it seems like you are learning React, Nextjs, Typescript, (Libraries), JSX, (Javascript), CSS, HTML.

That's way to much to start. So, start with the basics: Javascript, CSS, HTML. When you know them go in the advanced direction: JSX, React, important Libraries. And then go in the OP stuff like: NextJs and Typescript.

I learned all these stuff from this guy: [Codevolution](https://www.youtube.com/@Codevolution)
Very much recommended.

For the beginning I recommend you, you using his crash course, for the topics I wrote down: https://youtu.be/XIOLqoPHCJ4?feature=shared
Please do it, else you won't make it that long in programming
After this playlist you know the basics of webdevelopement and can start big projects like a ecommerce store with a db connenction

Enjoy the developement journey and best of luck ^^
Avatar
@gin https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
Avatar
Asian black bearOP
thanks, I'm not really stuck, i was just wondering after cloing the whole db why password was still null
const client = new Client({ ...db });
user was cloned correctly but not the password
I guess i could use db to query instead of client but I was just trying to figure out how things work under the hood