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
Project ID:
N/A
You might find these helpful:
- Deploy an ExpressJS app with PostgreSQL database
⚠️ experimental feature
Connection error? Query error? What?
N/A
I click on the button "buscaCliente"
And that's my "get" on my main
theres not a buscaCliente button in that html
but show me some errors please?
Here's the html
This is the message I get when I click on the button
Where is the front-end hosted
And could you show me the bit of code that starts express?
Do you have a repo for this?
Yes, I do
GitHub
GitHub - kevinuzan/http-nodejs
Contribute to kevinuzan/http-nodejs development by creating an account on GitHub.
So
I just put the postgre on the same enviroment as the server, and did what I found on railway
And with that, I get a response correctly
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
you shouldn't be making queries from the client side
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
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
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
you send the name to the backend in a post request
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
Yess, that's what I'm trying to do
But I'm not managing how to do it
whats your backend domain?
I didn't understand the question
Sorry
your railway url for the project
Use fetch instead of xhr
i was just preparing them a fetch example lol
Lool. Nic3
nice
How so?
Oh okay, thanks
on the client do something like this
and on the backend you can get that name by doing this in your handler
Take out the then and catch await
oh shit i forgot
Might also need a content type header
Application/json
Why do I have to take off the then?
please hold
Alright
fixed
thank you finn
Thanks
And on my backend, should I use post?
As you are sending the method post?
yes it would be a app.post handler
OKay
I'm getting this as an error message
Do you have a "clienteData" post handler?
Yes
Show me please
If thats on a new deployment then it might take a bit to switch over
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
Oh I'm blind, you where making a GET request to an endpoint that only handles POST requests
But isn't it a POST request?
And the backend is also an app.post
method is supposed to be lower case, sorry for my typo
Aright I'm gonna give it a try
Same error
I typed "post", but got the same thing
Oh
My bad hahahaha
Is "method"
Ah yes, I could have made that clearer
Aright, that worked just fine!
Thank you so much for ur help!
Can I ask one more thing? hahaha
Sure
Can't I have an onload function on my front end?
I tried to put it, so when they entered on this page, it would load some data, but it didn't work
Yeah but there are better ways to do it
Really?
I've built only electron apps, so I know just the basics...
At a glance this seems like a good resource
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...
Oh perfect! I'll try it out
Thank you so much
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
Yeah, I'm, but I just forgot to take it off
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
Yeah develop locally
Does it works simillary as electron?
Like, it's just update a function, and then update the webpage
?
sorry i dont quite know what you mean by that
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?
npm run start
then open it in your browser
And every time I make a change, I have to stop it, and then start it over again?
Or just click on "f5"?
sounds like you want vite
That sounded like u were ofering me drugs hahahah
yeah close enough
Honestly yeah
Hahahaah