OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

revenueKlaviyo_table is undefined in my Jquery code

  • Thread starter Thread starter minul hemasara
  • Start date Start date
M

minul hemasara

Guest
I'm working on a dashboard that fetches and displays revenue data from Klaviyo. I'm using jQuery and AJAX to get the data and populate a table. However, I'm encountering an issue where revenueKlaviyo_table is undefined. Here is the relevant part of my code:

Code:
<div class="box-header with-border white_bg">
    <h3 class="box-title">Revenue by Klaviyo</h3>
</div>
<div class="box-body">
    <table width="100%" id="klaviyoData" class="revenue-device-table">
        <thead>
            <tr>
                <th width="70%" class="text-right">Total</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>
<div class="overlay" id="revenueKlaviyo_preloader">
    <i class="fa fa-refresh fa-spin"></i>
</div>

let accessToken = '';

        $.ajax({
            url: "{{ route('klaviyo-authenticate') }}",
            type: "post",
            dataType: "json",
            success: function (data) {
                accessToken = data.access_token;
                
                // window.dashboard = new Dashboard(); 
                window.RevenueKlaviyo = new RevenueKlaviyo(window.dashboard, "https://api.cloudofgoods.com/api/v1/klaviyo/metrics/convertion-rate");
            },
            error: function (e) {
                console.log('Error fetching access token: ' + e);
            }
        });
            
        // Revenue Klaviyo 
        let RevenueKlaviyo = (function (dashboard, url) {
            let preloader, revenueKlaviyo_table;
            let status = false;

            if (dashboard === undefined) {
                dashboard = new Dashboard(); 
            }

            let init = function () {
                revenueKlaviyo_table = $('#klaviyoData tbody');
                preloader = $('#revenueKlaviyo_preloader');
                console.log("aaaa");
                populate();
            }

            let preloaderLoading = function () {
                preloader.html('<i class="fa fa-refresh fa-spin"></i>').show();
            }

            let preloaderSuccess = function () {
                preloader.hide();
            }

            let preloaderError = function () {
                preloader.html('<i class="fa fa-exclamation color-red"></i>').show();
            }

            let getStatus = function () {
                return status;
            }

            let populate = function () {
                console.log(accessToken);
                revenueKlaviyo_table.html('');
                $.ajax({
                    type: "get",
                    url: url,
                    data: {
                        dateText: dashboard.getDateText(),
                        customPickerFromDate: dashboard.getCustomPickerDateRanges().from,
                        customPickerToDate: dashboard.getCustomPickerDateRanges().to,
                        // interval: "day"
                        // city: dashboard.getCity()
                    },
                    headers: {
                        "Authorization": "Bearer " + accessToken,
                        // "Content-Type": "application/json"
                    },
                    beforeSend: function () {
                        preloaderLoading();
                        status = false;
                    },
                    success: function (response) {
                        let data = response.data;
                        if (data && data.total !== undefined) {
                            let total = data.total;
                            revenueKlaviyo_table.append(`<tr><td">${total.toFixed(2)}</td></tr>`);
                            preloaderSuccess();
                            status = true;
                        } else {
                            console.log('No data');
                            preloaderError();
                            status = true;
                        }
                    },
                    error: function () {
                        preloaderError();
                        status = true;
                    }
                });
            }

            let refresh = function () {
                populate();
            }

            return {
                init: init,
                refresh: refresh,
                getStatus: getStatus
            };
        });

I've ensured that the #klaviyoData table exists in the HTML. Can someone help me figure out why revenueKlaviyo_table is undefined and how to fix this issue?

<p>I'm working on a dashboard that fetches and displays revenue data from Klaviyo. I'm using jQuery and AJAX to get the data and populate a table. However, I'm encountering an issue where revenueKlaviyo_table is undefined. Here is the relevant part of my code:</p>
<pre><code><div class="box-header with-border white_bg">
<h3 class="box-title">Revenue by Klaviyo</h3>
</div>
<div class="box-body">
<table width="100%" id="klaviyoData" class="revenue-device-table">
<thead>
<tr>
<th width="70%" class="text-right">Total</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="overlay" id="revenueKlaviyo_preloader">
<i class="fa fa-refresh fa-spin"></i>
</div>

let accessToken = '';

$.ajax({
url: "{{ route('klaviyo-authenticate') }}",
type: "post",
dataType: "json",
success: function (data) {
accessToken = data.access_token;

// window.dashboard = new Dashboard();
window.RevenueKlaviyo = new RevenueKlaviyo(window.dashboard, "https://api.cloudofgoods.com/api/v1/klaviyo/metrics/convertion-rate");
},
error: function (e) {
console.log('Error fetching access token: ' + e);
}
});

// Revenue Klaviyo
let RevenueKlaviyo = (function (dashboard, url) {
let preloader, revenueKlaviyo_table;
let status = false;

if (dashboard === undefined) {
dashboard = new Dashboard();
}

let init = function () {
revenueKlaviyo_table = $('#klaviyoData tbody');
preloader = $('#revenueKlaviyo_preloader');
console.log("aaaa");
populate();
}

let preloaderLoading = function () {
preloader.html('<i class="fa fa-refresh fa-spin"></i>').show();
}

let preloaderSuccess = function () {
preloader.hide();
}

let preloaderError = function () {
preloader.html('<i class="fa fa-exclamation color-red"></i>').show();
}

let getStatus = function () {
return status;
}

let populate = function () {
console.log(accessToken);
revenueKlaviyo_table.html('');
$.ajax({
type: "get",
url: url,
data: {
dateText: dashboard.getDateText(),
customPickerFromDate: dashboard.getCustomPickerDateRanges().from,
customPickerToDate: dashboard.getCustomPickerDateRanges().to,
// interval: "day"
// city: dashboard.getCity()
},
headers: {
"Authorization": "Bearer " + accessToken,
// "Content-Type": "application/json"
},
beforeSend: function () {
preloaderLoading();
status = false;
},
success: function (response) {
let data = response.data;
if (data && data.total !== undefined) {
let total = data.total;
revenueKlaviyo_table.append(`<tr><td">${total.toFixed(2)}</td></tr>`);
preloaderSuccess();
status = true;
} else {
console.log('No data');
preloaderError();
status = true;
}
},
error: function () {
preloaderError();
status = true;
}
});
}

let refresh = function () {
populate();
}

return {
init: init,
refresh: refresh,
getStatus: getStatus
};
});
</code></pre>
<p>I've ensured that the #klaviyoData table exists in the HTML. Can someone help me figure out why revenueKlaviyo_table is undefined and how to fix this issue?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Ads by Eonads
Top