Nye
Nye

Reputation: 23

How to fix 'Private field must be declared in an enclosing class'

https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=d94b7d68284cbab1cdb7c2c3c81fd913&artist=kendrick+lamar&album=damn&format=json

The link above is a json file of an album and it has a URL of the album cover under the image section, but when I try extracting it, I get a syntax error telling me "Uncaught SyntaxError: Private field '#text' must be declared in an enclosing class".

For the code below, to get the large image, I would expect to do data.album.image[2].#text, but whenever I do this, I get an error telling me this: "Uncaught SyntaxError: Private field '#text' must be declared in an enclosing class" (screenshot here: https://gyazo.com/f6c9a16af02bda42313b4f369a753c33). Is this because of the number symbol? Because when I do data.album.image[2].size, I get an expected output of large. If so, how can I work around it?

async function getAlbum() {
        AlbumName = localStorage.getItem("textvalue");
        ArtistName = localStorage.getItem("textvalue2");

        AlbumName = AlbumName.replace(" ", "+");
        ArtistName = ArtistName.replace(" ", "+");

        const response = await fetch(
          "https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=d94b7d68284cbab1cdb7c2c3c81fd913&artist=" +
            ArtistName +
            "&album=" +
            AlbumName +
            "&format=json"
        );

        const data = await response.json();

        const numberOfTracks = data.album.tracks.track.length;
        const AlbumImage = data.album.image[2].#text;
}

Upvotes: 1

Views: 20322

Answers (4)

Igor Kolganov
Igor Kolganov

Reputation: 1

Happens, when in a same class I declare method with same name vscode won't say anything about that. But error tells me that mistake on a different location.

So, try to find same declared function on one class or area and rename it.

Upvotes: 0

raquelhortab
raquelhortab

Reputation: 566

Not the answer to this specific question but in case someone gets here due to the same problem as me: By mistake I had imported a CSS file using a <script> tag instead of a <link> tag and the error was also Uncaught SyntaxError: Private field 'whatever' must be declared in an enclosing class

Upvotes: 1

dezbaz
dezbaz

Reputation: 41

This happened to me. I typed (#myDivId) in my event listener, I was supposed to type ("#myDivId"),

The whole incorrect line was:

var theParent = document.querySelector(#myDivId);

Was meant to be:

var theParent = document.querySelector("#myDivId");

Good luck! Dez

Upvotes: 4

Fred Klein
Fred Klein

Reputation: 658

You cannot access the #text property directly with the dot notation because it is a private field. You must use the bracket notation.

Replace the last line of your code with

const AlbumImage = data.album.image[2]['#text'];

more details here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields

and here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

Upvotes: 2

Related Questions