I am trying to create a graph in chart.js from data that i have extracted from an SQL database using python. I am creating a JSON file in python which I then load in javascript (I don't know if this is the best way to do this).
Here is my code
{% extends "layout.html" %}
{% block head %}
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>
{% endblock %}
{% block title %}
Graph
{% endblock %}
{% block main %}
<canvas id="chart" width="300" height="300"></canvas>
<script>
const labels = [];
const ycoordinate = [];
chartIt();
async function chartIt(){
await getData();
const ctx = document.getElementById('chart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets:[{
data: ycoordinate
}],
},
options: {}});
}
async function getData(){
const response = await fetch('data.json');
const data = await response.text();
console.log(data);
const labels = data.map((x) => x.date);
const ycoordinate = data.map((x) => x.mass);
}
</script>
{% endblock %}
Here is the format of my json file:
[{"date": "2020/07/28", "mass": 68.3}, {"date": "2020/07/29", "mass": 68.3}, {"date": "2020/07/30", "mass": 69.5},...]
My code works if a hardcode the json file into the JavaScript but as this will be changing as the users database I can't do this. When I run the code I get the following errors in my console:
Failed to load resource: the server responded with a status of 404 (NOT FOUND)
Uncaught (in promise) TypeError: data.map is not a function at getData (graph:92) at async chartIt (graph:66)