前提・実現したいこと
バックエンドにてGoogle chartsで作成したグラフ画像をサーバー内に保存したいがエラーが発生するので解決したい。
発生している問題
Google chartsをバックエンドで動かす手法として、npmで使える(モジュールという括り?) "google-charts-node" で動かしたがエラーが発生する。
提供されているgithub⇒ https://github.com/typpo/google-charts-node
動作環境
さくらVPS
Linux
CentOS8
Node.js
nodebrew
npm
google-charts-node
Javascript
該当のソースコード
Javascript
1◆sample.js・・・基本的に"google-charts-node" に記載されているExampleをそのまま適用 2 3const GoogleChartsNode = require('/root/node_modules/google-charts-node'); 4 5// Define your chart drawing function 6function drawChart() { 7 const data = google.visualization.arrayToDataTable([ 8 ['City', '2010 Population',], 9 ['New York City, NY', 8175000], 10 ['Los Angeles, CA', 3792000], 11 ['Chicago, IL', 2695000], 12 ['Houston, TX', 2099000], 13 ['Philadelphia, PA', 1526000] 14 ]); 15 16 const options = { 17 title: 'Population of Largest U.S. Cities', 18 chartArea: {width: '50%'}, 19 hAxis: { 20 title: 'Total Population', 21 minValue: 0 22 }, 23 vAxis: { 24 title: 'City' 25 } 26 }; 27 28 const chart = new google.visualization.BarChart(container); 29 chart.draw(data, options); 30} 31 32// Render the chart to image 33const image = await GoogleChartsNode.render(drawChart, { 34 width: 400, 35 height: 300 36});
エラー表示
CentOS8
1◆sample.js実行時のエラー表示 2 3/var/www/html/sample.js:32 4const image = await GoogleChartsNode.render(drawChart, { 5 ^^^^^ 6 7SyntaxError: await is only valid in async functions and the top level bodies of modules 8 at Object.compileFunction (node:vm:352:18) 9 at wrapSafe (node:internal/modules/cjs/loader:1031:15) 10 at Module._compile (node:internal/modules/cjs/loader:1065:27) 11 at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) 12 at Module.load (node:internal/modules/cjs/loader:981:32) 13 at Function.Module._load (node:internal/modules/cjs/loader:822:12) 14 at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) 15 at node:internal/main/run_main_module:17:47
怪しいと感じる点
◆その1◆
sample.js中の「const chart = new google.visualization.BarChart(container);」のcontainerに何か定義が必要かも?
通常Google chartsをフロンドエンドで使う際も同じようなコードを使う。
その際<div>の#id等を入れるが、バックエンドで使う際は何を入れるべきなのか?
◆その2◆
sample.js中の「const GoogleChartsNode = require('/root/node_modules/google-charts-node');」では正しくmoduleを読み込めてないかも?
しかし読込不能のエラーは出ていないので問題なし?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/10 17:15
退会済みユーザー
2021/08/10 23:27 編集
2021/08/11 10:57
退会済みユーザー
2021/08/11 11:20
2021/08/11 14:18