Error message in Android studio console for Angular JS: Unexpected token error, even though the code runs smoothly in browser

I'm encountering a strange issue with my Cordova Android project (AngularJS + Ionic). When I run the project in Android Studio, I receive an error on my JS file that says:

Uncaught SyntaxError: Unexpected token {, http://192.168.43.211:8100/templates/todo/js/controllers.js, Line: 184

However, when I run the same file in Google Chrome, it works fine without any errors.

Below is the code causing the error message:

$scope.todos = $scope.todos.map(({text, status, note, create_date, to_do_id}) => {
             var ch;
             if(status=='1') ch=true; else ch=false;
             console.log(ch);
                return {
                  text,
                  flag:ch,
                  note:note,
                  to_do_id:to_do_id,
                  create_date:create_date,
                  status:status
                  };
              });

If I hide the above code block, no error is shown in the Android Studio console. However, I need this code block to complete the functionality.

Any insights on what the problem might be?

Answer №1

Primary solution

Have you verified if your mobile browser actually supports JS arrow expressions? Since they were only introduced in ECMAScript 6, some browsers may not have incorporated support for them yet. You could attempt replacing the arrow expression with an anonymous function instead:

$scope.todos = $scope.todos.map(function(record) {
    var check;
    if (record.status == '1') check = true; else check = false;
    console.log(check);
    return {
        text: record.text,
        flag: check,
        note: record.note,
        to_do_id: record.to_do_id,
        create_date: record.create_date,
        status: record.status
    };
});

Alternative solution

If the initial code still fails to function as expected, you have the option of creating your own map function like so:

function _customMap(array, callback) {
    var newArray = [];

    for(var i = 0; i < array.length; i++) {
        newArray.push(callback(array[i]));
    }

    return newArray;
}

You can then utilize it similarly:

$scope.todos = _customMap($scope.todos, function(record) {
    var check;
    if (record.status == '1') check = true; else check = false;
    console.log(check);
    return {
        text: record.text,
        flag: check,
        note: record.note,
        to_do_id: record.to_do_id,
        create_date: record.create_date,
        status: record.status
    };
});

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

The pagination feature in React MUI DataGrid is malfunctioning

I am currently working with a datagrid and implementing server-side pagination. I have set a limit of 5 objects to be returned from the backend, and an offset variable to indicate from which index the next set of data should come. const handlePageChange = ...

React array mapping issue does not produce any error message

I have exhaustively searched through every answer on Stack Overflow in hopes of finding a solution to my problem. However, I find myself at an impasse as there are no error messages appearing in the console. It seems that there is a hidden bug eluding my d ...

Scrape data from websites where the main URL remains constant but the information in the table varies

If you take a look at this link, you'll notice that when the next page is selected, the table on the website gets reloaded with new content without any change in the URL. Even after inspecting the developer tools > Network > XHR, it's diffi ...

What is the process for including a JavaScript file in an HTML document?

Greetings to all and thank you for taking the time! I have a straightforward query for you.. I have designed an html page featuring only a basemap (open street map). Moreover, I possess a javascript file which utilizes your coordinates to calculate a perce ...

Tips for creating mocks/stubs for vue-i18n?

I have recently made the switch from Jest to Vitest as my unit testing library for my Vue 3 application. Currently, I am facing an issue while trying to write a unit test for a component that utilizes the vue-i18n library for text translation. When attemp ...

Running karma tests in an angularjs environment may require additional http requests from the configuration

Currently, I am in the process of setting up a Karma test suite using the mean stack as my code baseline. Specifically, I am focusing on writing tests for the login functionality. Here is what it looks like: (function() { describe('LoginController& ...

Node.JS executes Sandbox within a RESTful service environment

Utilizing the Node Restify Module to develop a REST service that accepts POST requests. Inside the service, I am attempting to create a Sandboxed process using the Node Sandbox module in order to execute dynamically inserted JavaScript without impacting th ...

Save information from an HTTP request made with a manually entered URL

As I delve into the world of web development, a particular issue has me stuck. My current project involves implementing a user password reset feature. The process goes like this: the user receives an email with a URL containing a unique token, clicks on th ...

The function Promise.all typically yields an array with nested arrays

Inside my node Router, the following code snippet is implemented: router.get("/single-base/:id", (req, res) => { Base.find({ _id: req.params.id }) .then(bases => { let basefetches = []; for (let base of bases) { ...

How can we prevent ng-include from triggering a submit action when combined with ng-click?

Here is an html template example: <div ng-controller="MyController"> <form novalidate> Name: <input type="text" ng-model="user.name" /> <button ng-click="greet(user)">GREET</button> </form> </div> I i ...

What causes my React app menu to unexpectedly open while simply updating the state without any CSS modifications?

In the Header component, there is a button that triggers the toggleNav function in context.js when clicked. This function changes the state of isNavOpen from false to true, resulting in the opening of the navigation. Surprisingly, there doesn't appear ...

Adjust the transparency and add animation effects using React JS

When working on a React project, I encountered an issue where a square would appear under a paragraph when hovered over and disappear when no longer hovered. However, the transition was too abrupt for my liking, so I decided to implement a smoother change ...

Creating a JavaScript function to download specific sections of an HTML page

Currently, I am implementing PHP MySQL in my HTML page. I have already utilized several div elements in my page. I successfully created a print button that prints a specific div on the page. Now, I am looking to add a download button that will allow users ...

Enhancing the javascript console output

Take a look at this demonstration: const y = "bar"; console.log(y); => y: bar Is there a way to modify console.log() or use a library like npm package to enhance its functionality? ...

The use of ReactDom.render is no longer permissible in Next.js

I just set up a fresh Next JS application using Next 12. Encountering this issue consistently on every page load in the browser: Alert: The use of ReactDOM.render is no longer supported in React 18. Please switch to createRoot instead. Until you make th ...

Using jQuery to update a specific item in a list

My current project involves developing an Image Gallery app. It uses <img> tags within li elements. The code snippet is as follows: var $slideR_wrap = $(".slideRoller_wrapper"); var $slidesRoller = $slideR_wrap.find(".slidesRoller"); var $slideR ...

Struggling to correctly showcase my Firebase data in Angular using the orderByChild method

I have a user database structured like this : Database - Users - Id (generated using the push method) - email - firstName - ptsTotal - ... I am looking to create a ranking of users based on their total points (ptsTotal) in a game using ...

Is Nextjs prone to creating identical pages during the build process?

I have a question that I couldn't find the answer to anywhere, so I hope someone here can help me. I have a blog and I want to use SSG for the homepage and ISR for individual posts. If I set up my homepage using SSG to display 10 posts like this: ind ...

Obtain layers that have been specifically chosen

Is there a method to retrieve an array of currently selected layers without the need to iterate through all the layers and potentially altering the selection? This differs from the question mentioned here. function get_selected_layers() { var layers = ...

What is the best practice for integrating Angular controllers into an ASP.NET MVC application?

When developing an application in ASP.NET MVC and Angular, it's important to carefully consider where to store Angular scripts. Typically, the main application files containing Angular modules are placed inside the Scripts directory and referenced in ...