Ionic - Error: SyntaxError: Unexpected token caught

I am currently utilizing Ionic Framework version 1.7.16

The error on Android is pinpointed to the following line of code

var obj = {[$myid] : firebase.database.ServerValue.TIMESTAMP};

No error occurs when the line above is changed to the following

var obj = {'test':'test'};

Controller Code:

.controller('SearchStateCtrl', function($scope, $firebaseObject, $firebaseArray, $location, $stateParams, my_details) {
  $scope.stateId = $stateParams.stateId;
  $scope.my_details = my_details;

  var user = firebase.auth().currentUser;
  $scope.my_id = user.uid;

  var database = firebase.database();
  var trips = database.ref('Trips/'+$stateParams.stateId);

  trips.on('value', function(snapshot) {
    $scope.my_trips = $firebaseArray(trips);
  }, function(err) {
     console.log("Something is wrong.");
  });

  $scope.addToMyTrip = function ($trip, $myid) {
    var tripRef = database.ref('MyTrips');
    var obj = {[$myid] : firebase.database.ServerValue.TIMESTAMP};
    tripRef.set(obj).catch(function(error) {
      console.log('Somthing went wrong.');
    });
    var tripRef = database.ref('MyTrips/'+$trip.$id);
    tripRef.set({
      'i-am': $trip['i-am'],
      'trip-date': $trip['trip-date'],
      'state-from': $trip['state-from'],
      'city-from': $trip['city-from'],
      'state-to': $trip['state-to'],
      'city-to': $trip['city-to'],
      'user-id': $trip['user-id']
    }).catch(function(error) {
      console.log('Somthing went wrong.');
    });
  }
})

The error on Android seems to be caused by the dynamic key name, as everything works without any issues when viewed on the webpage using

ionic serve --lab

https://i.sstatic.net/UTdSv.png

Answer №1

Emmanuel Delay was instrumental in resolving the issue by suggesting a change in the code:

var obj = {[$myid] : firebase.database.ServerValue.TIMESTAMP};

The updated code is as follows:

var key = [$myid]; 
var obj = {}; 
obj[key] = firebase.database.ServerValue.TIMESTAMP;

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 use of 'android.disableResourceValidation=true' in React Native is considered experimental and not officially supported

I'm currently working on a React Native application and encountering an error while trying to create an Android APK file. WARNING: The option setting 'android.disableResourceValidation=true' is experimental and unsupported. The current defa ...

Rotating a loaded file using THREE.js

After experimenting with the ThreeJS library for a few weeks and drawing inspiration from others, I managed to create a canvas that displays an external .stl-file. However, my main issue is that I am struggling to apply any transformations to it. While I ...

When working with NodeJS and an HTML form, I encountered an issue where the 'Endpoint'

Having trouble sending input data from a form to my nodejs endpoint. When I try printing the req.body, it shows up as undefined and I can't figure out why. Here is the relevant API code snippet: var bodyParser = require('body-parser') var e ...

An error occurred due to an unexpected identifier, '_classCallCheck', while the import call was expecting exactly one

Encountering an unexpected identifier '_classCallCheck'. Import call requires precisely one argument. Having trouble with React Native. I have attempted every solution found on the internet, but none proved successful. Is there any way to upgrade ...

Gson in action - managing and accessing an ArrayList containing complex objects

I have a class named NameAndPosition that stores the name and position of users. I've created an ArrayList to hold objects of type NameAndPosition. ArrayList<NameAndPosition> LocSenders = new ArrayList<NameAndPosition>(); Using the Gson ...

Guide to obtaining every conceivable index combination in a sequential manner for the same attribute within an object array

I have an array of objects structured like this: [{ property1: 10 }, { property1: 13 }, { property1: 15 }, { property2: 2 }] I am looking to create a function that will generate an object containing all possible index combinations (from left to right) of ...

Sophisticated web applications with Ajax functionalities and intricate layouts powered by MVC frameworks

I am looking to integrate an ajax-driven RIA frontend, utilizing JQuery layout plugin (http://layout.jquery-dev.net/demos/complex.html) or ExtJs (http://www.extjs.com/deploy/dev/examples/layout/complex.html), with... a PHP MVC backend, potentially using ...

Leveraging body-parser alongside formidable

I am currently in the process of integrating formidable to handle forms that involve file uploads (specifically images). Despite comments suggesting otherwise, I came across a comment demonstrating successful integration of both modules. This snippet show ...

Arranging the Bars in a Bar Chart Using Chart.JS

Lately, I've been playing around with ChartJS and encountering an issue with sorting the bars in descending order, from lowest to highest. Despite my efforts to troubleshoot, I haven't had any success in resolving it. ...

Running a code from a plugin in Wordpress site

I am currently utilizing the "wp-video-lightbox" plugin for WordPress, which generates small floating boxes for my videos. I am interested in incorporating variables like http://www.example.com/?video3 to provide shortcuts similar to what YouTube offers. ...

Utilize a variable beyond the scope of an ajax success callback

Within a single function, I have a series of timeout functions that execute asynchronously. function myFunction() { var data; setTimeout(function(){ $.ajax({ //My Ajax Stuff success: function(data) { var data = ...

Passing arguments to the callback function in React: a comprehensive guide

Within my react component, I have a collection of elements that I want to make clickable. When clicked, I trigger an external function and pass the item ID as an argument: render () { return ( <ul> {this.props.items.map(item => ( ...

How to activate a media query in response to user interaction using JavaScript

My responsive web page has various designs for different screen sizes, implemented using @media queries. I am interested in allowing users to manually adjust the design for smaller or larger screens without actually changing the screen size. Is there a wa ...

Why won't setInterval function properly with innerHTML?

I've been attempting to include a small feature on my website that displays the running seconds, but for some reason my JavaScript function isn't working. Strangely, it's not throwing any errors either. Here's the code I'm using: f ...

How to increment a date by 1 in an AngularJS function

I have a table with 5 cells in the header, each representing a different week (for example: Week 01, Week 02, and so on). In the first cell, the week is displayed like this: <div class="monthCells">Week {{vm.selectedPeriod}}</div> and it sho ...

Encountering an npm install error during the creation of an ionic app on Windows 10 despite having all required dependencies installed and environment variables properly configured

view error screenshot npm version 5.3.0 node version 8.4.0 cordova version 7.0.1 ionic version 3.9.2 Below are my environment variables: C:\Program Files (x86)\Java\jdk1.7.0_17\bin;C:\Android_SDK_latest\platform-tC:\An ...

Executing a function from another reducer using React and redux

My application consists of two main components: the Market and the Status. The Status component manages the user's money, while the Market component contains buttons for purchasing items. My goal is to decrement the user's money when a button in ...

This function appears to have an excessive number of statements, totaling 41 in total

Currently, I am using this controller: .controller('ctrl', function($scope, $rootScope, $timeout, $alert, $location, $tooltip, $popover, BetSlipFactory, AccordionsFactory, AuthFac ...

Implementing dynamic controls in an ASP.NET MVC5 application using AngularJS

I am currently working on a project that will utilize MVC5, angularJS, and bootstrap for development. One of the key features in my project is a dropdown menu called "expense type". Upon selecting a value from this dropdown, additional controls need to be ...

I'm wondering how I can design a utility function within my Redux module that can extract a specific subset of read-only data from the current state

I am currently utilizing redux to create a "helper function" inside my redux module that is responsible for fetching filtered data from the state based on a specified index. This specific data will be used to generate a form consisting of inputs depending ...