DEV Community

Discussion on: How to get data from an MySQL database in React Native

Collapse
 
saulojoab profile image
Saulo Joab

Hey, you're on the right path! If I understood correctly, you want to get values from your form and then send them to your connection.query string on the server, right?

To do that on ReactJS/React Native, all you gotta do is send a JSON object with the request configuration and your data. The JSON object with your data is called 'body'. Here's how you should do it:

async test(){
    await fetch('http://yourPCip:3000/newUser', {
      method: 'POST', // Here you're saying that you want to make a POST request. Could be any method, like a GET, for example.
      headers: '', // You can specify your requisition headers here. That line is optional.
      body: { // Here's the fun part. Put your data here.
        "name": this.state.name,
        "email": this.state.email,
        "password": this.state.password
      }
    })
    .then(response => response.json()) 
    .then(serverResponse => console.warn(serverResponse))
  }

If you really want to get the hang of it, I recommend you to study NodeJS (which is the server we're using). It's amazing and super useful!

Collapse
 
gijshendriksen03 profile image
GijsHendriksen03 • Edited

Hey man, I've been trying to get a insert to work but it won't work. I got this error in the query. And know I'm stuck. Any tips?

Collapse
 
cphilipse profile image
CPhilipse • Edited

Yes that's what I want! Now I understand how it should work, I only have troubles with implementing it. I put my project on GitHub: github.com/CPhilipse/httprequest
I've put that code of yours in my Registration class, because that's where the state is. Now I call this async function when I click on the register button, but then I get this error: Possible Unhandled Promise Rejection (id: 0):TypeError: Cannot read property 'name' of undefined. I've looked for it on the internet and it's likely that it's because it takes time for the values to get in the state, which is why it won't find it. But when I console.log these values in a different function which I call when I click on register, it does just find it. So I've my doubts that that's the problem. I've put my PC ip in there, so it isn't that. Since it's a promise, it has to be from that function, but what am I missing?

Thread Thread
 
saulojoab profile image
Saulo Joab

I created a PR there, check it out.

Thread Thread
 
cphilipse profile image
CPhilipse

That's amazing! It works! I don't understand how those changes you made, calling the function differently, catch error and adding a header made it work, but thanks man! This is pretty cool! I've got one more question though, if I understand correctly this method/communication with the database will only work when the node server is started through this command: node Routes.js. Now let's say this application is going live and anyone can download this app. How is it supposed to work for those downloading it? Do I have to automatically make this command run for those people?
Also, with this code: await fetch('my_ip:3000/newUser', | Can I just leave this be? And will it still work for everyone? I guess I understand it working on localhost, but when everyone has the app, it's not localhost anymore that can work, right?
(I hope you can enlighten me on this. I'm not that familiar with the depths of networking, so excuse me if this a newbie question. Thanks in advance )

Thread Thread
 
saulojoab profile image
Saulo Joab

I'm also a newbie, don't worry.

If you want other people to use your app, all you gotta do is host your database and your NodeJS server somewhere.
Tip: You can host your Node server on Glitch or Heroku for free.

Then, instead of inserting your computer ip on the fetch function, you would insert the hosted Node server URL :)

I highly recommend you to study node, and also to check out Heroku and Glitch.

Thread Thread
 
cphilipse profile image
CPhilipse

Okay, that makes so much sense. I'll definitely check it out. Appreciate your help man!