【问题标题】:JavaScript chart.js syntax error if statementJavaScript chart.js 语法错误 if 语句
【发布时间】: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


【解决方案1】:

箭头函数应该有花括号而不是括号。尝试用这个替换data

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;
    }
}),

注意箭头后面的花括号和最后一行括号前面的花括号。

【讨论】:

  • {} Uncaught SyntaxError: missing ) after argument list error.
  • 对不起。那是因为我是打字员。当我尝试执行该过程时,没有发生错误,但数组的变量仍然是[]。为什么会这样?
  • 看起来month 永远不会更新,所以它总是等于空字符串。所以你的 else 语句中的代码永远不会运行。
【解决方案2】:

在地图中使用{} 而不是()

应该是这样的:

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) => {
                    console.log("user", 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
  })

【讨论】:

  • {} Uncaught SyntaxError: missing ) after argument list error.
  • 您的代码有误。请找到它并按照我说的去做。
  • 给你。我为你做的。
  • 对不起。那是因为我是打字员。当我尝试执行该过程时,没有发生错误,但数组的变量仍然是[]。为什么会这样?
  • 数据在用户中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-22
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多