简体   繁体   English

JavaScript错误-未捕获的SyntaxError:意外令牌{

[英]JavaScript Error - Uncaught SyntaxError: Unexpected token {

I am trying to use Chart JS in one of my laravel applications. 我试图在我的laravel应用程序之一中使用Chart JS。

I am pushing the data through the route and using json_encode($gross) to echo the gross of each order by day but I am getting the following error in my console log: 我通过路由推送数据,并使用json_encode($ gross)逐日回显每个订单的毛额,但是在控制台日志中出现以下错误:

Uncaught SyntaxError: Unexpected token { 未捕获到的SyntaxError:意外令牌{

It references the the line of where I have used json_encode($gross). 它引用了我使用json_encode($ gross)的行。 Anyone have any ideas why this is happening? 任何人都知道为什么会这样吗?

Here is the code coming through the controller:

public function index()
{   $wkRevenue = \App\Order::where('created_at', '>=' , \Carbon\Carbon::now()->startOfMonth())->get();
    // dd($wkRevenue->pluck('created_at','gross'));

    return view('admin.dashboard')
            ->with('created_at', $wkRevenue->pluck('created_at'))
            ->with('grosss', $wkRevenue->pluck('gross'));
}

Here is the code in the js file:

var data = {
    type:'line',
    labels:['Mon','Tues','Wed','Thurs','Fri','Sat','Sun'],
    datasets:[
    {
        data: {!! json_encode($gross) !!},
        backgroundColor:'rgba(137, 200, 85, 0.4)',
    }
]
}
var graph = document.getElementById('myNewChart').getContext('2d');
var myNewChart = new Chart(graph ,{
type: "line",
data: data,
options:{
    title:{
        display:true,
    }
}
}); 

You have a typo in your controller: 您的控制器中有错字:

->with('grosss', $wkRevenue->pluck('gross'));

needs to be: 需要是:

->with('gross', $wkRevenue->pluck('gross'));

Laravel changed the pluck() method in 5.2 to return a collection instead of an array. Laravel在5.2中更改了 pluck pluck()方法以返回集合而不是数组。

In your view change your json_encode() to: 在您的视图中,将json_encode()更改为:

{!! $gross->toJson() !!}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM