Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Svelte reference global variable inside onMount error

So basically I initialize an object within onMount like this-

onMount(() => {
    
    const mapy = new mapboxgl.Map({
      container: "map",
      style: "mapbox://styles/mapbox/satellite-v9",
      center: testCoords[0],
      zoom: 19,
    
    });
    console.log(mapy);
});

Then outside I have-

function AddPoint(){
    Center=mapy.getCenter;
}

AddPoint is called from a HTML button. I get this error in the console- Uncaught ReferenceError: mapy is not defined

I only press the button after it has been initialized, and I know it has been initialized because of my console.log() inside onMount

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is a straightforward scope error. When you declare mapy inside onMount (const mapy = ...), you make mapy local to onMount in scope, meaning it is undefined outside of onMount.

What you want to do here is declare mapy at the top level of your script, then assign to it inside onMount:

let mapy;

onMount(() => {
    mapy = new mapboxgl.Map({
      container: "map",
      style: "mapbox://styles/mapbox/satellite-v9",
      center: testCoords[0],
      zoom: 19,    
    });
    console.log(mapy);
});

function AddPoint() {
    Center = mapy ? mapy.getCenter : undefined;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!