0
votes

I want to validate the form then pass form data to firebase database using react hook form but I'm stuck here its not working what i pass in fetch body? and why its not working

    import React from "react";
    import "./App.css";
    import { useForm } from "react-hook-form"; 
    import classNames from "classnames";
    import { useState } from "react";
    
    function App() {
      const { register, handleSubmit,formState: { errors } } = useForm();
      // console.log(errors);
      const onSubmit = data => console.log(JSON.stringify(data));
   const postData = async (e)=>{
     e.preventDefault();
     const res = await fetch("https://test1-5022f-default- 
           rtdb.firebaseio.com/reactformData.json",
    {
      method:"POST",
      header:{
        "content-type":"application/json",

      },

What is written here in body??

      body:JSON.stringify(data)
    }
    
    )
  };

There are many fields in form but here i show some

    return (
            <div>
        
        <div className="container">
              
              <div className="form-group my-3 ">
                <form name="Registration_form" id="Form" action="" method="POST" onSubmit={handleSubmit(onSubmit)}>
                  
                  <div className="form-group my-3">
                    <label htmlFor="name">Name:<span style={{fontWeight:"bold",color:"red"}}>*</span></label>
                   
                    <input 
                      type="text" 
                      name="Name" 
                      id="Name" 
                      className={classNames("form-control",{"is-invalid":errors.Name,})}  
                      autoComplete="off" 
                      {...register('Name', 
                      { required: true,
                        maxLength: 15,
                        pattern: /^[A-Za-z]+$/
                      
                      })
                      }
        
                      
                      />
        
                    <span id="name" className="text-danger fw-bold">{errors.Name?.type === "required"  && "This field is required"}</span>
        
                    <span id="name" className="text-danger fw-bold">{errors.Name?.type ==="maxLength" && "Length Should be less then 15"}</span>
        
                    <span id="name" className="text-danger fw-bold">{errors.Name?.type === "pattern"  && "Digits are not allow"}</span>
        
                  </div>
    
    <div className="form-group my-3">
                <label htmlFor="email">Email:<span style={{fontWeight:"bold",color:"red"}}>*</span> </label>
                
                <input 
                  type="text" 
                  name="email" 
                  id="email" 
                  className={classNames("form-control",{"is-invalid":errors.email,})}  
                  placeholder="[email protected]" 
                  autoComplete="off" 
                  {...register('email', 
                  { 
                    required: true,
                    pattern:/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
                    
                  
                  })
                  }
                
                  />
    
                  <span id="mail" className="text-danger fw-bold">{errors.email?.type === "required"  && "Email is required"}</span>
    
                  <span id="mail" className="text-danger fw-bold">{errors.email?.type === "pattern"  &&"Invalid format"}</span>
    
               
              </div>

 <input type="submit" id="submit" value="submit" onClick={postData} className="btn btn-success my-3" />
        </form>

can anyone find mistake in code hoe can I connect and send this form data to fire base real time data base using react hook form because first I want validation using react hook form then send the entire data to data base. so how it possible??