title: Passing value from backend nodejs to frontend react native
body: <p>My front end is done in React Native and backend in nodejs. It is an application that asks the user to register (email, password, name, email, ...) and then the data is sent to a database (mongodb) using mongoose.</p>
  <p>In my front end; when the user presses the button SignUp, it will call a function names &quot;Submit&quot; that you can find below:</p>
  <pre><code>    const Submit = async (fname, email, pwd, confpwd) =&gt; {
    if (String(pwd).localeCompare(String(confpwd)) == 0) {
        let result = await fetch('http://127.0.0.1:2000/api/user/register', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(
          {
            name: fname.fname,
            email: email.email,
            password: pwd.pwd
          }
        )
      })
      console.log(&quot;RESULT : &quot; + JSON.stringify(result))
    } else{
      console.log('NOT SAME PASSWORD')
    }
    
    
  };
  </code></pre>
  <p>It just takes in the firstname, email and password entered by the user and uses fetch to post it to the API. Everything works, except the last line : console.log(&quot;this is the result : &quot; + JSON.stringify(result)). It always returns an empty json.</p>
  <p>The register route in my backend is the following:</p>
  <pre><code>//REGISTER
  router.post('/register', async (req, res) =&gt; {
  
      //Check the input of the user before we make a user
      
      const {error} = registerValidation(req.body)
      if (error) {
          console.log('Error1')
          return 'Error1'
      }
      console.log('1&amp;')
      //Check if the user is already in the database
      const emailExist = await User.findOne({email : req.body.email});
      if(emailExist) {
          console.log('Error2')
          return 'Error2'
      }
      console.log('2&amp;')
      //Hash the password
      const salt = await bcrypt.genSalt(10);
      const hashedPassword = await bcrypt.hash(req.body.password, salt)
      console.log('3&amp;')
      //Create a new user
      const user = new User({
          name: req.body.name,
          email: req.body.email,
          password: hashedPassword
      })
      console.log('4&amp;')
  
      //user.save(); 
  
      try{
          const saveUser = await user.save();
          res.send(saveUser);
          console.log('5&amp;')
      }catch(err){
          res.send(err)
      }
  });
  </code></pre>
  <p>Before saving it to the databse it checks the validation and if the email already exists in the database.</p>
  <p>It successfully sends the data to the database when the first two conditions are met and if email is already used and the validation format is not correct it does not put it in the dataabse.</p>
  <p>However, when the validation format is not correct or the emaoil is already used, I would like my fetch in my front end to let me know. So I thought that putting my fetch in a variable it would output something if it did not work. However, it always sends back an empty json even when the fetch did not work. So, how can I pass a variable from my backend to my frontend ?</p>
  <p>Normally in postman this is what I receive when the email already exists. How can I receive this on my frontend ?</p>
  <p><a href="https://i.stack.imgur.com/YK09X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YK09X.png" alt="enter image description here" /></a></p>