this javascript ' let streetLatitude; // Define streetLatitude globally let…
- User
this javascript ' let streetLatitude; // Define streetLatitude globally let streetLongitude; // Define streetLongitude globally let marker; // Define marker globally to make it accessible across functions let data;
function fetchStreetDetails() { fetch("main.json") .then((response) => response.json()) .then((data) => { const entryCount = data.length; // data is already an array of objects const streetDetails = data[0]; // Extract street details streetLatitude = streetDetails.StreetLatitude; streetLongitude = streetDetails.StreetLongitude; streetHeading = streetDetails.StreetHeading; streetPitch = streetDetails.StreetPitch; streetPanoID = streetDetails.StreetPanoID; const StreetPoints = streetDetails.Points; const panorama = new google.maps.StreetViewPanorama( document.getElementById("streetview"), { position: { lat: streetLatitude, lng: streetLongitude }, pano: streetPanoID, heading: streetHeading, pitch: streetPitch, } ); console.log("Street Latitude: " + streetLatitude); console.log("Street Longitude: " + streetLongitude); console.log("Street Heading: " + streetHeading); console.log("Street Pitch: " + streetPitch); console.log("Street PanoID: " + streetPanoID); console.log("Street Location: " + StreetPoints); // Update numberoffeeds div // Update numberoffeeds div const numberoffeedsElement = document.getElementById("numberoffeeds"); numberoffeedsElement.textContent = `There are ${entryCount} separate questions.`; }) .catch((error) => console.error("Error fetching data: ", error)); } fetchStreetDetails(); const startingLocation = { lat: 51.540073, lng: -0.010874 }; // London Aquatics Center coordinates function initMap() { const zoom = 8; const map = new google.maps.Map(document.getElementById("map"), { center: startingLocation, zoom: zoom, mapId: "DEMO_MAP_ID", }); // Function to add marker on click function addMarker(event) { const clickLocation = event.latLng; marker = new google.maps.Marker({ position: clickLocation, map: map, draggable: true, // Set draggable to true }); // Remove the click listener after adding a marker google.maps.event.removeListener(clickListener); // Add functionality after clicking the map createSubmitButton(clickLocation); } // Create a function to add the submit button function createSubmitButton(distance, clickLocation) { const buttonsDiv = document.getElementById("buttons"); // Check if the button already exists before creating a new one if (!document.getElementById("submit")) { const submitButton = document.createElement("button"); submitButton.id = "submit"; submitButton.textContent = `Submit`; // Add event listener for the submit button (you can define the functionality here) submitButton.addEventListener("click", async () => { console.log("Submit button clicked!"); // Create the new button - Assistant
The error message 'Cannot read properties of undefined (reading 'findIndex') at handleNextClick' indicates that the
datavariable is not being properly accessed inside thehandleNextClickfunction.To fix this issue, you need to pass the
datavariable as a parameter to thehandleNextClickfunction. Inside thecreateSubmitButtonfunction, modify the event listener for thenextButtonto pass thedatavariable to thehandleNextClickfunction like this:nextButton.addEventListener("click", async () => { handleNextClick(data); });Then, inside the
handleNextClickfunction, make sure to use thefetchedDataparameter instead of directly accessing thedatavariable like this:async function handleNextClick(fetchedData) { // Fetch details for the next entry const currentEntryIndex = fetchedData.findIndex(entry => entry.Name === streetDetails.Name); // Rest of the code }By making these changes, you should be able to access the variable inside the function without encountering the 'Cannot read properties of undefined' error.
- User
- Assistant
The error 'cannot read properties of undefined (reading 'findIndex')' is occurring because the
streetDetailsobject is undefined inside thehandleNextClickfunction. To resolve this error, you need to make sure that thestreetDetailsobject is accessible within thehandleNextClickfunction.One way to address this issue is to pass the
streetDetailsobject as a parameter to thehandleNextClickfunction when calling it. Here's how you can modify your code:- Inside the
createSubmitButtonfunction, include thestreetDetailsobject as an additional parameter for thehandleNextClickfunction:
submitButton.addEventListener('click', async () => { console.log("Submit button clicked!"); nextButton.addEventListener('click', async () => { handleNextClick(data, streetDetails); }); });- Modify the function to accept the object as a parameter:
- Inside the
- User
here is my submitButton.addEventListener - ' submitButton.addEventListener("click", async () => { console.log("Submit button clicked!"); // Create the new button const nextButton = document.createElement("button"); nextButton.id = "nextButton"; nextButton.textContent = "Next"; // Customize button text as needed
// Add event listener for the new button (optional, if needed)
nextButton.addEventListener('click', async () => { handleNextClick(data, streetDetails);});' This gives an error 'streetDetails is not defined at HTMLButtonElement.'
- Assistant
The error 'streetDetails is not defined at HTMLButtonElement' occurs because the
streetDetailsvariable is not accessible within the event listener function for thenextButton. To resolve this issue, you need to ensure that thestreetDetailsvariable is available in the scope where the event listener is defined.One way to address this is by storing the
streetDetailsobject in a global variable that can be accessed from within the event listener function. Here's an example of how you can modify your code to achieve this:- Define a global variable to store the
streetDetailsobject:
let streetDetails; // Define streetDetails globally- Assign the
streetDetailsobject inside thethenblock of thefetchStreetDetailsfunction:
.then((data) => { // Assign streetDetails object streetDetails = data[0]; // Rest of your code }) - Define a global variable to store the