MD. Jubair Mizan
MD. Jubair Mizan

Reputation: 1570

Javascript variable not working inside obejct . But getting on console.log

Javascript variable does not work inside an object. I see the data when I console.log(dataPondsRevenue) variable, but getting the error:

SyntaxError: missing ] after element list`!

when I use it inside the data:[] node:

$('.pondsRevenueBlock').on('click',function(){
    var block_id = $(this).attr('data-id');
    var url='{{ route('WhiteFish.client.pondsRevenueBlockWise') }}';
    $.ajax({
        url:url+'?block_id='+block_id,
    }).done(function(pondsRevenueData){
        var dataPondsRevenue = '';
        $.each(pondsRevenueData, function(index, element) {
            dataPondsRevenue+= '{value:'+element.pondTotalInvest+',name:'+element.name+'},';
        });
        console.log(dataPondsRevenue);

        var eChart_2 = echarts.init(document.getElementById('pondsRevenue'));
        var option1 = {
            tooltip : {
                backgroundColor: 'rgba(33,33,33,1)',
                borderRadius:0,
                padding:10,
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: 'rgba(33,33,33,1)'
                    }
                },
                textStyle: {
                    color: '#fff',
                    fontStyle: 'normal',
                    fontWeight: 'normal',
                    fontFamily: "'Open Sans', sans-serif",
                    fontSize: 12
                }   
            },
            // color: ['#0FC5BB', '#92F2EF', '#D0F6F5'],
            color: ['#0FC5BB', '#0FC5BB', '#5AC4CC'],
            series : [
                {
                    name: 'task',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '50%'],
                    roseType : 'radius',
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)",
                        backgroundColor: 'rgba(33,33,33,1)',
                        borderRadius:0,
                        padding:10,
                        textStyle: {
                            color: '#fff',
                            fontStyle: 'normal',
                            fontWeight: 'normal',
                            fontFamily: "'Open Sans', sans-serif",
                            fontSize: 12
                        }   
                    },
                    data:[
                        console.log(dataPondsRevenue);
                    ],
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        eChart_2.setOption(option1);
        eChart_2.resize();
    }).fail(function (data) {
        console.log('error');
    });
});

How can I solve it?

Upvotes: 1

Views: 75

Answers (4)

user10622929
user10622929

Reputation:

You are creating a JSON string. This could be parsed into an object using JSON.parse() but that seems unnecessary over complication as you could create the required array of objects to start with:

var dataPondsRevenue = [];
$.each(pondsRevenueData, function(index, element) {
   dataPondsRevenue.push({value: element.pondTotalInvest, name: element.name});
});

Upvotes: 0

wscourge
wscourge

Reputation: 11311

Use an array (and optionally JSON.stringify it; in case of $.ajax, have a look at contentType at $.ajax docs), it is much less error-prone - in your case, there's always a trailing comma at the end, which is not a valid JSON:

console.log("Valid:")
console.log(JSON.parse('{ "whatever": 1 }'))
console.log("Invalid:")
console.log(JSON.parse('{ "whatever": 1, }'))

$('.pondsRevenueBlock').on('click',function(){
    var block_id = $(this).attr('data-id');
    var url='{{ route('WhiteFish.client.pondsRevenueBlockWise') }}';
    $.ajax({
        url:url+'?block_id='+block_id,
        contentType: 'application/json'
    }).done(function(pondsRevenueData){
        var dataPondsRevenue = [];
        $.each(pondsRevenueData, function(index, element) {
            dataPondsRevenue.push({
              value: element.pondTotalInvest,
              name: element.name
            })
        });
        console.log(dataPondsRevenue);

        var eChart_2 = echarts.init(document.getElementById('pondsRevenue'));
        var option1 = {
            tooltip : {
                backgroundColor: 'rgba(33,33,33,1)',
                borderRadius:0,
                padding:10,
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: 'rgba(33,33,33,1)'
                    }
                },
                textStyle: {
                    color: '#fff',
                    fontStyle: 'normal',
                    fontWeight: 'normal',
                    fontFamily: "'Open Sans', sans-serif",
                    fontSize: 12
                }   
            },
            // color: ['#0FC5BB', '#92F2EF', '#D0F6F5'],
            color: ['#0FC5BB', '#0FC5BB', '#5AC4CC'],
            series : [
                {
                    name: 'task',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '50%'],
                    roseType : 'radius',
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)",
                        backgroundColor: 'rgba(33,33,33,1)',
                        borderRadius:0,
                        padding:10,
                        textStyle: {
                            color: '#fff',
                            fontStyle: 'normal',
                            fontWeight: 'normal',
                            fontFamily: "'Open Sans', sans-serif",
                            fontSize: 12
                        }   
                    },
                    data: JSON.stringify(dataPondsRevenue),
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        eChart_2.setOption(option1);
        eChart_2.resize();
    }).fail(function (data) {
        console.log('error');
    });
});

Also, the console.log(), returns undefined - you can just pass the variable instead.

Upvotes: 2

marekful
marekful

Reputation: 15361

There seems to be quite a confusion as to how to create the needed data object. With the code

$.each(pondsRevenueData, function(index, element) {
    dataPondsRevenue+= '{value:'+element.pondTotalInvest+',name:'+element.name+'},';
});

You are creating a JSON string. This could be parsed into an object using JSON.parse() but that seems unnecessary over complication as you could create the required array of objects to start with:

var dataPondsRevenue = [];
$.each(pondsRevenueData, function(index, element) {
    dataPondsRevenue.push({value: element.pondTotalInvest, name: element.name});
});

Then, just assign dataPondsRevenue to data:

...
},
data: dataPondsRevenue,
itemStyle:
...

Upvotes: 1

Duncan Thacker
Duncan Thacker

Reputation: 5188

This is because console.log(dataPondsRevenue) is a function that returns undefined, so

data: [ console.log(dataPondsRevenue) ]

means

data: [ undefined ]

You should do

data: [ dataPondsRevenue ]

to get the actual data into the array.

Upvotes: 1

Related Questions