Home > database >  Visual Studio bundler causes JS Uncaught ReferenceError: Cannot access variable before initializatio
Visual Studio bundler causes JS Uncaught ReferenceError: Cannot access variable before initializatio

Time:12-10

I've got an AJAX request as follows:

$.ajax({
  method: 'GET',
  url: '/api/some-data',
  headers: {
    'Content-Type': 'application/json'
  },
  success: function(data) {
    
     if (data != null) {
      var userData = data;
      for (var key of Object.keys(userData)) {

        if (userData[key].BalanceYear == null) {
          var accYear = "Today";
        } else {
          var accYear = userData[key].BalanceYear;
        }
      // ETC, ETC, ETC

This works fine. However, when I bundle it in Visual Studio it converts to:

$.ajax({
    method: "GET",
    url: '/api/some-data',
    headers: {
        "Content-Type": "application/json"
    },
    success: function(n) {
        var t, i, r, u, f, e;
        if (n != null) {
            t = n;
            for (i of Object.keys(t))
                r = t[i].BalanceYear == null ? "Today" : t[i].BalanceYear,
                u = yearsArr.findIndex(n=>n === r),
                u === -1 && (yearsArr.push(r),
                yearsArr.sort(function(n, t) {
                    return n - t
                }));
            const o = new Map;
            for (const {AccountName: n, Amount: i, BalanceYear: t} of t)
                o.has(n) || o.set(n, yearsArr.map(()=>0)),
                f = t ? yearsArr.indexOf(t) : yearsArr.indexOf("Today"),
                o.get(n)[f] = i;
            function c(n, t) {
                return v(n) || a(n, t) || h(n, t) || l()
            }
            // ETC, ETC, ETC

The issue I get is:

Uncaught ReferenceError: Cannot access 't' before initialization

on the line:

for (const {AccountName: n, Amount: i, BalanceYear: t} of t)

Since I don't really have control over t I'm not sure what to do to fix this, particularly as t is declared above the problem line (I also tried to declare t outside of the AJAX function too).

Would anyone know what I could do to fix this?

CodePudding user response:

I found this was due to the const here

for (const {AccountName: n, Amount: i, BalanceYear: t} of t)

I removed it and it seems to work now. I don't know the logic behind this unfortunately.

  • Related