簡體   English   中英

JavaScript錯誤-未捕獲的SyntaxError:意外令牌{

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

我試圖在我的laravel應用程序之一中使用Chart JS。

我通過路由推送數據,並使用json_encode($ gross)逐日回顯每個訂單的毛額,但是在控制台日志中出現以下錯誤:

未捕獲到的SyntaxError:意外令牌{

它引用了我使用json_encode($ gross)的行。 任何人都知道為什么會這樣嗎?

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

您的控制器中有錯字:

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

需要是:

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

Laravel在5.2中更改了 pluck pluck()方法以返回集合而不是數組。

在您的視圖中,將json_encode()更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM