【发布时间】:2021-04-29 09:26:49
【问题描述】:
我们正在使用 chart.js 库。使用的框架是来自 Rails 的 Slim。我正在使用 JavaScript。
我得到一个错误:
Uncaught SyntaxError: Unexpected token 'if' error in the if(user.activity_type === "groupA").
script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"
javascript:
var users = #{raw @user.to_json}
let count = 0;
let month = "";
let array = [];
var ct = document.getElementById('ex_chart');
var ex_chart = new Chart(ct, {
type: 'horizontalBar',
data: {
labels: ["Jan", "Feb", "Mar", "Apr"],
datasets: [
{
data: users.map((user) => (
if(user.activity_type === "groupA") {
if(month === "" || month === user.month) {
count += user.count;
} else {
array.push(count);
count = user.count;
month = user.month;
}
return array;
}
),
backgroundColor: '#ff7f50'
}},
{
data: [880, 740, 900, 520, 930],
backgroundColor: '#ff6347'
}
]
});
},
options: options
})
var options = {
scales: {
xAxes: [{
ticks: {
min: 300
}
}]
}
};
【问题讨论】:
-
检查你的括号,
data: users.map((user) => (应该是data: users.map((user) => {
标签: javascript syntax-error slim