The program encountered an unexpected identifier 'getProject' instead of ';'. It was expecting to find a semicolon after the async variable declaration

When using this JavaScript on a webpage, I encounter an issue:

<script async type="module">
import {projectCode} from "./assets/js/config.js";
import {getProject} from "./assets/js/saleproject.js";
import {getAccount} from "./assets/js/account.js";
import Vue from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e282b3b1e6c7068706f6a">[email protected]</a>/dist/vue.esm.browser.js'

let projectData = await getProject(projectCode);

...

</script>

To render a page in Vue.js, the data fetched is stored. The code referred to above is found in a separate file:

import { server } from './config.js';

export async function getProject(projectCode) {
    const response = await fetch(server + '/saleproject/GetByCode/' + projectCode);
    const projectData = await response.json();
    return projectData;
}

While everything functions correctly on desktops, iPhones present an error that interrupts Vue.js rendering:

SyntaxError: Unexpected identifier 'getProject'. Expected ';' after variable declaration.

An attempt was made to resolve it by enclosing it in an asynchronous function, but still encountered difficulties:

(async function () {
    let projectData = await getProject(projectCode);
}())

This resulted in a Vue.js error:

Error in data(): "ReferenceError: projectData is not defined"

Adjustments are needed to ensure proper functioning on iPhones (currently works on desktop Safari).

Answer №1

Your solution attempt is accurate, but the problem lies in the fact that the let variable is confined to the IIFE scope.

To address this, you should modify your code as shown below:

getProject(projectCode).then(projectData => {
 // Insert all code within this block
});

The original issue stems from the absence of top-level await functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a form in a React component that allows the user to input information

My challenge is to create a functionality where each time the add button is clicked, it should dynamically add an input field that I can save. It's similar to this example, but with inputs instead. The complexity arises from the fact that the inputs a ...

Confirming the accuracy of multiple fields in a form

Looking for help with validating both password and email fields on a registration form. I've successfully implemented validation for passwords, but need assistance adding validation for the email section as well. Can both be validated on the same form ...

Double the fun: JavaScript array appears twice!

I am currently working on displaying the selected filters from checkboxes. Initially, I create an array containing the values selected from the checkboxes and then aim to add them to a string. Suppose I have two checkboxes labeled: label1 and label2, my a ...

"Is there a way to retain the value of a variable outside of an ajax success

I've been working on some form validation functions and here's what I've come up with: <script> $(document).ready(function() { var dataObject = {}; $('#username').keyup(function () { id = $(this).attr('id'); ...

Using jQuery to load content with a dynamic variable instead of specific element IDs

I am facing a minor jQuery issue. I am trying to load a specific area of an external HTML document into a div, but instead of loading just that particular area, the entire document is being loaded. Here's the code snippet for the trigger: $('a& ...

Unable to execute internal functional tests due to this error: [POST http://localhost:4444/wd/hub/session] unable to connect - ECONNREFUSED

Currently working with node 0.12 and intern 3 in order to execute functional tests, but encountering the following error: SUITE ERROR Error: [POST http://localhost:4444/wd/hub/session] connect ECONNREFUSED at Server.createSession <node_m ...

Vue.js encountered a problem with chokidar stating: "Error: EBUSY: resource busy or locked, lstat 'C:hiberfil.sys'"

Description Whenever I execute the command npm run serve, I encounter the following error: ERROR Failed to compile with 1 error 15:34:19 This dependency was not found: * ... in ./node_ ...

Enable the choice for Bootstrap collapse animation to be customized

Is there a way to allow users or admins on my website to decide whether or not Bootstrap 4 collapse elements should be animated? The concern is that when many elements are moved by the animation, it becomes less smooth. Therefore, it would be ideal to give ...

Is the screen size detection malfunctioning when using Vue 3's computed property?

How do I accurately detect screen size in Nuxt 3? I have tried using the code below, but the screen size always shows as 1200. It seems that the computed property is being executed before the onMounted() hook. How can I resolve this issue? <template> ...

Implementing append operations in JavaScript without relying on the id attribute

Is there a way to specify the second div without assigning it an ID or using any attributes, and then perform an append operation inside it? For instance, indicating that the p element should be appended within the second div without relying on an ID or ...

Validation of a string or number that is not performing as expected

I have been working with the yup library for JavaScript data validation, but I am encountering unexpected behavior. Despite thoroughly reviewing their documentation, I cannot pinpoint where I am misusing their API. When I run the unit test below, it fails ...

Troubleshooting the issue of "Mismatched transaction number*" in MongoDB and Node.js

While trying to add data, I encountered an issue with modifying two schemas using ACID transactions in MongoDB with Node.js. Upon running the program, an error was displayed: (node:171072) UnhandledPromiseRejectionWarning: MongoError: Given transaction n ...

Why does my Redux callback keep getting invoked multiple times?

In developing a react application with redux, I have chosen to avoid using react-redux by manually handling all dispatched events. Below is a sample code snippet. The content of index.html <!DOCTYPE html> <html> <head> <script src=& ...

Incorporate h:outputText into your JavaScript code segment

After populating some information into a h:outputText, I am now looking to trigger a JavaScript function upon clicking a button that will retrieve the text from the outputText. The structure of the code is as follows: <p:tabView id="tabView"> & ...

I wonder what the outcome would be if I used res.send to send a JSON file instead of res.json

Is it possible to send a JSON file using res.send in NodeJs instead of res.json? What are the potential consequences of doing this and why is it recommended to avoid this practice? ...

Creating a PDF document with multiple pages using a PHP loop, integrating with AJAX, and utilizing the m

Looking for assistance with a plugin development project involving PDF generation using AJAX. The challenge lies in generating multiple PDFs for each user within a loop. The goal is to create PDFs with multiple pages for individual users. If that's n ...

Highlighting the menu item in AngularJS based on a randomly generated address

Here is a code snippet that I need help with: Html <li ng-class="{ highlight: isActive('/admin/trackDef/list') || isActive('/admin/trackDef/add')}"> <a href="${createLink(uri: '/#/admin/trackDef/list')}"></ ...

Encountering an error while attempting to launch an AngularJS application on Node.js? Let's

Currently, I am in the process of learning Angular. Whenever I attempt to run my code, an error message pops up: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7a737a7c6b6d70715f2b312f3115">[email protected]< ...

Ways to retrieve parameters in getStaticPaths function?

I'm currently working on a Next.js app with Contentful as the CMS. The file structure relevant to my question is: pages -[category] -[slug].js My goal is to access the category value when a user visits category/slug. Currently, I have the category ...

What is the best way to display a string state value in a React component?

I need assistance with displaying a state value that is in string format. Despite my efforts, I have not been successful in finding a solution and am feeling quite frustrated. Can anyone provide some guidance? ...