Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

Javascript chart.js data object/array

i want to load in a data array for my chart like this:

data: chardata

I create a variable like this:

var chartdata = [];

i fill it with data from a function which should return an array but it returns an object

function getChartData(type) {
var chartdata = [];
console.log(typeof chartdata);
getDates(14).forEach(element => {
    var lastdate = new Date(element);
    lastdate.setHours(23);
    lastdate.setMinutes(59);
    lastdate.setSeconds(59);
    modlogRef.where("userName", "==", modname).where("action", "==", type).where("timestamp", ">=", element).where("timestamp", "<=", lastdate)
        .get()
        .then(function(querySnapshot) {
            chartdata.push(querySnapshot.size);
        })
        .catch(function(error) {
            console.log("Error getting documents: ", error);
        }); 
});
console.log(typeof chartdata);
return chartdata;

and when i look what type it is it says object put it shoud be a array? so when i return it to the chart and set data to chardata it fails because it is a object not an array? I even tried converting it to an array like this:

Object.values(chardata)

but that is just an empty array even if the object had values? What am i doing wrong? The error that i get is:

[email protected]:7 Uncaught TypeError: Cannot read property 'skip' of undefined
    at ce ([email protected]:7)
    at fe ([email protected]:7)
    at me ([email protected]:7)
    at ni.getElementsAtEventForMode ([email protected]:7)
    at ni.handleEvent ([email protected]:7)
    at ni.eventHandler ([email protected]:7)
    at i ([email protected]:7)
    at HTMLCanvasElement.Fe.<computed> ([email protected]:7)
question from:https://stackoverflow.com/questions/65863791/javascript-chart-js-data-object-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First in javascript an array is an object, to know if something is an array you should use Array.isArray(varName).

It looks like you get an empty array back because your query to get data returns nothing, I quickly pushed in a for loop some values to the array and it returns it fine so it must be your dataset which does not contain anything. Or your filters make it return nothing

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...