GET information from postgres server

I am building an app, and I want to get information from my postgreSQL server, so I have the requisition on my main page, and I want to get the response on my index.js, but I cannot manage to get it on my index.js
85 Replies
Percy
Percy2y ago
Project ID: N/A
Percy
Percy2y ago
You might find these helpful: - Deploy an ExpressJS app with PostgreSQL database
⚠️ experimental feature
Brody
Brody2y ago
Connection error? Query error? What?
KChammas
KChammasOP2y ago
N/A
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
I click on the button "buscaCliente"
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
And that's my "get" on my main
Brody
Brody2y ago
theres not a buscaCliente button in that html but show me some errors please?
KChammas
KChammasOP2y ago
Here's the html
KChammas
KChammasOP2y ago
This is the message I get when I click on the button
Brody
Brody2y ago
Where is the front-end hosted And could you show me the bit of code that starts express?
KChammas
KChammasOP2y ago
Brody
Brody2y ago
Do you have a repo for this?
KChammas
KChammasOP2y ago
Yes, I do
KChammas
KChammasOP2y ago
GitHub
GitHub - kevinuzan/http-nodejs
Contribute to kevinuzan/http-nodejs development by creating an account on GitHub.
KChammas
KChammasOP2y ago
So I just put the postgre on the same enviroment as the server, and did what I found on railway
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
And with that, I get a response correctly
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
But I want to send the query from my index.js I think the problem is the way I'm reading the data Sending*] I think the real problem for me, is: How to send data with the HTTPRequestHeader Shouldn't it be on the req
Brody
Brody2y ago
you shouldn't be making queries from the client side
KChammas
KChammasOP2y ago
I'm just gonna send the data, like the condition to search on the database For now, I would like to know how to send a string on it
Brody
Brody2y ago
you really shouldn't be sending query strings from the frontend, please trust me on that. that looks like some sort of form if im not mistaken, so you would want to post that form data to the backend, then handle that data there, make the required db queries in the backend, process the data returned from the db query then return only the necessary data to the frontend
KChammas
KChammasOP2y ago
But like, on my app, you have to say the name that it should search on the DB, how would I get the name if it's not with the frontend
Brody
Brody2y ago
you send the name to the backend in a post request
KChammas
KChammasOP2y ago
Here it will be a list of names, and then when I select one, and click on "buscar" it should return all the data from this customer
KChammas
KChammasOP2y ago
Yess, that's what I'm trying to do But I'm not managing how to do it
Brody
Brody2y ago
whats your backend domain?
KChammas
KChammasOP2y ago
I didn't understand the question Sorry
Brody
Brody2y ago
your railway url for the project
Finn
Finn2y ago
Use fetch instead of xhr an
Brody
Brody2y ago
i was just preparing them a fetch example lol
Finn
Finn2y ago
Lool. Nic3 nice
KChammas
KChammasOP2y ago
How so? Oh okay, thanks
Brody
Brody2y ago
on the client do something like this
const resp = await fetch('https://danig-budget.up.railway.app/cliente?name=' + 'brody', {
method: 'POST',
headers: {
"Content-Type": "application/json",
}
});

// Handle any errors please

const data = await resp.json();

console.log(data);
const resp = await fetch('https://danig-budget.up.railway.app/cliente?name=' + 'brody', {
method: 'POST',
headers: {
"Content-Type": "application/json",
}
});

// Handle any errors please

const data = await resp.json();

console.log(data);
and on the backend you can get that name by doing this in your handler
let name = req.query.name;
let name = req.query.name;
Finn
Finn2y ago
Take out the then and catch an await an
Brody
Brody2y ago
oh shit i forgot
Finn
Finn2y ago
Might also need a content type header Application/json
KChammas
KChammasOP2y ago
Why do I have to take off the then?
Brody
Brody2y ago
please hold
KChammas
KChammasOP2y ago
Alright
Brody
Brody2y ago
fixed thank you finn
KChammas
KChammasOP2y ago
Thanks And on my backend, should I use post? As you are sending the method post?
Brody
Brody2y ago
yes it would be a app.post handler
KChammas
KChammasOP2y ago
OKay
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
I'm getting this as an error message
Brody
Brody2y ago
Do you have a "clienteData" post handler?
KChammas
KChammasOP2y ago
Yes
KChammas
KChammasOP2y ago
Brody
Brody2y ago
Show me please If thats on a new deployment then it might take a bit to switch over
KChammas
KChammasOP2y ago
It's not, it's just one that I had like 20 minutes ago The deployment is taking only 1 minute to complete and be available
Brody
Brody2y ago
Oh I'm blind, you where making a GET request to an endpoint that only handles POST requests
KChammas
KChammasOP2y ago
But isn't it a POST request?
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
And the backend is also an app.post
Brody
Brody2y ago
method is supposed to be lower case, sorry for my typo
KChammas
KChammasOP2y ago
Aright I'm gonna give it a try Same error I typed "post", but got the same thing Oh My bad hahahaha Is "method"
Brody
Brody2y ago
Ah yes, I could have made that clearer
KChammas
KChammasOP2y ago
Aright, that worked just fine! Thank you so much for ur help! Can I ask one more thing? hahaha
Brody
Brody2y ago
Sure
KChammas
KChammasOP2y ago
Can't I have an onload function on my front end?
KChammas
KChammasOP2y ago
I tried to put it, so when they entered on this page, it would load some data, but it didn't work
Brody
Brody2y ago
Yeah but there are better ways to do it
KChammas
KChammasOP2y ago
Really? I've built only electron apps, so I know just the basics...
Brody
Brody2y ago
At a glance this seems like a good resource
Brody
Brody2y ago
Working with onload events in vanilla JS
Understanding how onload event works in the DOM with vanilla JS is a very important piece of knowledge for a web developer. In this artic...
KChammas
KChammasOP2y ago
Oh perfect! I'll try it out Thank you so much
Brody
Brody2y ago
No problem, happy coding! Also, just one more thing, you're storing the database url as a variable in the JavaScript code, this is a gigantic no no, that url is available as a environment variables on railway, and locally too if you run railway shell
KChammas
KChammasOP2y ago
Yeah, I'm, but I just forgot to take it off
KChammas
KChammasOP2y ago
KChammas
KChammasOP2y ago
I'm not using, thanks for the reminder I just found out that if the DB is on the same enviroment as the server, I don't need to say the url But I'll search it uo for the variables as well Also, is it better to run it locally? Is is faster? Bc running it on the railway, I have to update the GIT, make a commit, and then push origin, and then wait 2 minutes
Brody
Brody2y ago
Yeah develop locally
KChammas
KChammasOP2y ago
Does it works simillary as electron? Like, it's just update a function, and then update the webpage ?
Brody
Brody2y ago
sorry i dont quite know what you mean by that
KChammas
KChammasOP2y ago
For example, on the railway/git development, I have to update on git and then wait to deploy on the web When I'm using locally, how should I "update" to see the changes?
Brody
Brody2y ago
npm run start then open it in your browser
KChammas
KChammasOP2y ago
And every time I make a change, I have to stop it, and then start it over again? Or just click on "f5"?
Brody
Brody2y ago
sounds like you want vite
KChammas
KChammasOP2y ago
That sounded like u were ofering me drugs hahahah
Brody
Brody2y ago
yeah close enough
Finn
Finn2y ago
Honestly yeah
KChammas
KChammasOP2y ago
Hahahaah
Want results from more Discord servers?
Add your server