So my web app is a combination of Django, React and BigQuery. What I want to do is to make a POST request from React to Django, and Django will fetch the table data from BQ before sending the table data back as an array to the React frontend. How can I achieve this? What I have so far:
app.js
const handleConfirm = () => {
fetch("http://localhost:8000/get-data-instance/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(tableName),
});
};
views.py
def get_data_instance(request):
client = bigquery.Client()
if request.method == 'POST':
table_requested = json.loads(request.body)
query_string = f"""
SELECT *
FROM `project-id.dataset-id.{table_requested}`
"""
result = (
client.query(query_string)
.result()
)
records = [dict(row) for row in result]
data_instance = json.dumps(str(records))
return render(request, 'frontend/index.html')
I basically want to send back the data_instance created above.
If I try to print data_instance, i get the correct output
>> "[{'key': 1, 'Date': '2021-11-10', 'Hour': 1, 'Measurement': 2.0}]"
So, now I'm just stuck on how can I pass this data back to React?
Note: I'm not using DRF for this project.