I am trying to do a test task and I am prohibited from using libraries and plugins to work with DB, ORM.
I need to get data from a PostgreSQL table and display it in the React component, but I don't know how to fetch data from the node to react.
Here is how I get the data from the database:
const { Client } = require("pg");
const client = new Client({
host: "localhost",
user: "postgres",
port: 5432,
password: "postgres",
database: "welbex-test",
});
client.connect();
client.query(`SELECT * from products`, (err, res) => {
if (err) {
console.log(err.message);
} else {
console.log(res.rows);
}
client.end;
});
Here is how I am trying to fetch data:
useEffect(() => {
const fetchProducts = async () => {
setLoading(true);
const res = await axios.get(
"http://localhost:3000/src/Components/API/connect"
);
setProducts(res.data);
setLoading(false);
};
fetchProducts();
}, []);