0

so recently ive been programming my first React JS project, but ive been running into a few problems when trying to reach my backend and retrieve data from my database. I think its probably a very silly mistake only a rookie like me could make, but ive tried for hours now and just cant figure it out. any help appreciated :)

tried fetch() with GET and POST with parameters in the body aswell as a paramstring, aswell as some Axios stuff ive seen some indian fellow do in a tutortial, none have worked so far. I think my backend seems to be fine because if i hardcode the parameter it works fine. so my guess is that my problem has something to do with the parameters

fetch 'GET'

    async function checkEmailTaken(email) {
        await fetch("/api/emailtaken?" + new URLSearchParams({ email: email }).toString())
                .then(res => res.json())
                .then(data => {
                    console.log(data);
                    if(data.length > 0) return true;
                }); 

fetch 'POST'

    async function checkEmailTaken(email) {
        await fetch("/api/emailtaken", {
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                email: email, 
            }) 
        })
        .then(res => res.json())
        .then(data => {
            console.log(data);
            if(data.length > 0) return true;
        }); 
    }

have also tried with async so i fairly confident thats not it

Backend:

const express = require('express');
const app = express();
const mysql = require('mysql');

const db = mysql.createPool({
    host:"localhost",
    user:"root",
    database:"skinport-bot"
});

app.use(express.json());

app.post("/api/emailtaken", (req, res) => {
    const email = req.params.email

    const sql = "SELECT * FROM user WHERE email = ?";
    db.query(sql, [email], (error, result) => {
        if (error) throw console.log(error);
        res.send(result);
    });
});

app.listen(3001, () => {
    console.log("running on port 3001");
});

0 Answers0