search

Home  >  Q&A  >  body text

React - JS Api: Resolving undefined return

I'm new to React and js and I'm having a little trouble understanding how Promise and resolve work.

I'm trying to use the API to log into an application that has a SQL internal database. The queries are fine and return what they need to return, but strangely the parsing of the json data doesn't end up working and returns it as undefined.

The following is the current relevant code in the API:

userAuth = (identifiant, mdp) => {
    return new Promise((resolve, reject) => {
      config.query(
        "SELECT identifiant, mdp FROM utilisateur WHERE identifiant = '"+identifiant+"' and mdp = '"+mdp+"'",
        (error, utilisateur) => {
          if (error) {
            return reject(error);
          }
          //une petite manipulation de donnée pour éviter des soucis de format par la suite.
          return resolve(utilisateur);
        }
      );
    });
  };
  
   
  
  app.post("/auth", (req, res) => {
    console.log("connection");
    const data = {
      identifiant: req.body.Identifiant,
      mdp: req.body.Password,
    };
    console.log(data);

    userAuth(data.identifiant, data.mdp)
      .then((response) => {
        console.log(response);
        res.send(response);
      })

      .catch((error) => {
        console.log(error);
        res.status(500).send("Erreur serveur");
      });
  });

Here is the relevant code from my application:

const handleSubmit = async (event) => {
        event.preventDefault();
        const id = formAuth.Identifiant;
        const password = formAuth.Password;
        if(!password.match(re)){alert("Format de mot de passe incorrect")}
        else {
            const response = await postAuth();
            console.log(response);
            if (response.lenght != 0){
                navigAcc('/Accueil');
            }
            else{
                alert("Identifiant ou mot de passe incorrect")
            }
        }
    }

const postAuth = async () => {
        const body = {
          Identifiant: formAuth.Identifiant,
          Password: formAuth.Password
        };      
        const config = {
          headers: {
            'Content-Type': 'application/json'
          }
        };
        const response = (await axios.post(api+"auth", body, config));
        setFormAuth({
            Identifiant:'',
            Password:''
        })
      };

I found another similar question on the site (Why is router undefined in React js?), but I didn't see any actual meaningful difference between what I'm doing and the submitted answer, So I feel very lost.

I tried parsing the api response in json, even though it was already a type error, I tried treating the response from the application as an array, I tried treating it as a boolean based on whether it was empty or not empty, see if I Is it possible to not care what exactly is inside, since it will only be filled if the validation is valid. I don't really see the problem, but I know it's specifically on userAuth 's Promise because it seems to be the only thing that creates or rephrases the error when I touch it.

P粉253800312P粉253800312233 days ago382

reply all(1)I'll reply

  • P粉777458787

    P粉7774587872024-04-04 13:44:35

    I forgot to return it after postAuth.

    const postAuth = async () => {
        const body = {
          Identifiant: formAuth.Identifiant,
          Password: formAuth.Password
        };      
        const config = {
          headers: {
            'Content-Type': 'application/json'
          }
        };
        const response = (await axios.post(api+"auth", body, config));
        setFormAuth({
            Identifiant:'',
            Password:''
        })
        return response;
      };

    reply
    0
  • Cancelreply