1

I need to access my Data from my mysql Database using express, on my server the data is as a json, but when i try to access it i always get 'undefined' and my express server crash

the json i have on the server : [{"idProjet":1,"nomProjet":"test","dateDebut":"2021-05-18T22:00:00.000Z","nomAuteur":"mathieu","prenomAuteur":"jean","organisme":"idmc"}]

fetching code :

let id = 'id :';
    const [data, setData] = useState([]);

    useEffect(() => {
        fetch('http://localhost:3000/projets')
            .then(response => {return response.json()})
            .then((json => {console.log(json);setData(json);}))
            .catch(error => console.error(error));
        console.log(data);
    }, []);

Route.js code :

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');

const connection = mysql.createPool({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'agora'
});

// Starting our app.
const app = express();

// Creating a GET route that returns data from the 'users' table.
app.get('/projets', function (req, res) {
    // Connecting to the database.
    connection.getConnection(function (err, connection) {

        // Executing the MySQL query (select all data from the 'users' table).
        connection.query('SELECT * FROM projet', function (error, results, fields) {
            // If some error occurs, we throw an error.
            if (error) throw error;

            // Getting the 'response' from the database and sending it to our route. This is were the data is.
            res.send(results)
        });
    });
});

// Starting our server.
app.listen(3000, () => {
    console.log('Go to http://localhost:3000/projets so you can see the data.');
});
Kyoui
  • 35
  • 6
  • Can you add your express server code? – Nicolas Castellanos May 19 '21 at 14:33
  • i added it, but i took that code from a tutorial tho – Kyoui May 19 '21 at 14:51
  • @Kyoui Did you check if your server is running without issues? As you created "app" object in Route.js, did you modify your App.js to utilise "Route.js" for app object? I don't think this is problem of React Native, this has something to do on your server side. – Satyam May 19 '21 at 15:08
  • the fact that i can access it with the localhost url means that the serve rworks right ? i don't call the route.js in my app.js, i thought they were 2 separate thing, like on one terminal i start the node route.js and on the other terminal i expo start my app – Kyoui May 19 '21 at 15:23

1 Answers1

0

The most common problem for this type of behavior is that you are using react-native on an android emulator. Android Emulator is using an IP-address different from localhost on windows machine. For more information, check here the official documentation.

So you can forward your port on the same port used by the android emulator (10.0.2.2) or you can change the port to 80 so you won't have any problem You can go check this answer here

lemon
  • 14,875
  • 6
  • 18
  • 38