Skip to Content
Menu
This question has been flagged

Hello, I am trying to create a chart snippet using data from the controller and using that data to create a Chart snippet.
Here is what I have managed to do and I am getting this error from the console.


Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)

Controller:

@http.route ( '/btc/usd' , auth = "public" , type = "json" , methods =[ 'POST' , 'GET' ] , csrf = False ) 
def all_bitcoin_usd ( self , **kwargs ):
print ( 'Calling this method >>>' )
# fetch_usd = http.request.env['usd.btc.exchange'].search_read([], ['name', 'date', 'amount_paid'])

fetch_usd = http.request.env[ 'usd.btc.exchange' ].search([])
for i infetch_usd:
print ( 'fetch usd' , i)
param = {}
data = dict ()
data[ 'name' ] = i.name
data[ 'date' ] = i.date
data[ 'amount_paid' ] = i.amount_paid
param[ 'param' ] = data
n = json.dumps(param)
print ( 'nnn' , n)
return Response(json.dumps(param , default =date_utils.default) , content_type = 'application/json;charset=utf -8' ,status = 200 )

js file

odoo.define( 'dynamic_jobs_snippets.s_bitcoin_chart_temp2' , function (require) { 
var PublicWidget = require( 'web.public.widget' );
var rpc = require( 'web.rpc' );
var Dynamic = PublicWidget.Widget.extend( {
selector: '.usd_bitcoin_chart_can2' ,
start: function () {
var self = this ;
rpc.query({
route: '/btc/usd' ,
params: {},
}).then((result) => {
console .log(result);
const myChart = new Chart({
type: 'line' ,
data: {
labels: result.param[ 'name' ],
datasets: [{
label: 'My First Dataset' ,
data: result.param[ 'amount_paid' ],
fill: true ,
backgroundColor: 'rgba(255, 99, 132, 0.2)' ,
borderColor: 'rgb(255, 99, 132)' ,
pointBackgroundColor: 'rgb(255, 99, 132)' ,
pointBorderColor: '#fff' ,
pointHoverBorderColor: 'rgb(255, 99, 132)'
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});

});
},
});
PublicWidget.registry.usd_bitcoin_chart_can2 = Dynamic;
return Dynamic;
});


Canvas UI

id="usd_bitcoin_chart " height="400px" width="400px"/>




Avatar
Discard
Best Answer

Hi,

In your controller, it seems like there is a variable mismatch. You are assigning the result of search_read() to the values ​​variable, but you are returning fetch_usd instead. You should change return fetch_usd to return values ​​to return the data fetched from the server.

@http.route('/btc', auth='public', type='json', methods=['POST', 'GET'], csrf=False) def all_bitcoin_usd(self, **kwargs):
print
( 'Calling this method >>>')
values ​​= http.request.env['usd.btc'].search_read([], ['name', 'date', 'amount'])
print('fetch usd ', values)
return values


In addition to the variable mismatch, you may also want to make sure that the data you are returning from your controller is in the expected format in your JavaScript code. Currently, you are accessing fetch_usd['date'] and fetch_usd['amount'] , but your server-side code is returning a list of dictionaries with keys 'name' , 'date' , and 'amount' .


Hope this will help you

Thanks

Avatar
Discard
Author

Hi Mehjabin Farsana,
I have updated my code kindly check, and I am getting this error "Object of type date is not JSON serializable", dispite including default =date_utils.default.
I am also not loading the data from the Javascript end coz I am getting this error from the console:
pattern_waves_3.svg: Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR).

I am also getting these errors from the console:
bitcoin.s_bitcoin_chart already defined
at odoo.define (web.assets_frontend_minimal.min.js:22:29)
at web.assets_frontend_lazy.min.js:10325:131
at web.assets_frontend_minimal.min.js:9:63
at new Promise (<anonymous>)
at processJob (web.assets_frontend_minimal.min.js:9:9)
at Object.processJobs (web.assets_frontend_minimal.min.js:17:1)
at odoo.define (web.assets_frontend_minimal.min.js:23:293)
at web.assets_frontend_lazy.min.js:10325:131
at web.assets_frontend_minimal.min.js:9:63
at new Promise (<anonymous>)

What could be the issue?

Related Posts Replies Views Activity
1
Apr 24
1292
0
Jan 24
1029
1
Sep 23
2228
2
Apr 23
4230
2
Jan 23
6730