0
   const onSubmit = (data) => {
    output ? setOutput(false) : setOutput(true)
    sendData(data)
  }


  async function sendData(data) {
    await axios({
      url:"someurl",

      data: { data },
      headers: {
        'Access-Control-Allow-Origin': '*'
      },
    });
  }

Here I am calling SubmitData function and passing to it some data from front-end, and then calling SendData function to post this data via axios to firebase real-time database. I didn't create backend of the app, so instead tried to learn doing API requests using firebase. Can anybody please help me to figure this out? Many thanks ahead!

1 Answers1

0

From this example: https://firebase.google.com/docs/database/rest/save-data#section-post

It looks like you'll need to figure out how your data is stored as JSON.

Here is an example from the docs: https://firebase.google.com/docs/reference/rest/database#section-post

Your sendData function can possibly be modified as follows:

  async function sendData(data) {
    await axios({
      method: 'post'
      url:'https://<your-project-id>.firebaseio.com/<your-file>.json',
      data: { data },
      headers: {
        'Access-Control-Allow-Origin': '*'
      },
    });
  }

Hope this helps.

Aastha Bist
  • 324
  • 2
  • 11
  • Hello, I have tried your solution, but still axios is not working. Console shows these errors: 1) Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at "myurl". (Reason: CORS preflight response did not succeed). Status code: 405. 2) Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://test2-ed616-default-rtdb.asia-southeast1.firebasedatabase.app/. (Reason: CORS request did not succeed). Status code: (null). – Nurali Sapargaliev Sep 05 '22 at 16:45
  • 3) Uncaught (in promise) Object { message: "Network Error", name: "AxiosError", code: "ERR_NETWORK", config: {…}, request: XMLHttpRequest, response: XMLHttpRequest, stack: "" } – Nurali Sapargaliev Sep 05 '22 at 16:48
  • I think the following answer should help with CORS: https://stackoverflow.com/questions/46963855/firebase-realtime-database-cors-issue – Aastha Bist Sep 05 '22 at 17:10