Need Help on an issue when using ionic in Android


#1

I am currently trying out my app using a samsung tablet SM-T113NU. When I try ionic run android, i am getting just a white screen. So, i opened up chrome inspect and I got a syntax error saying there is an unexpected token “(” in line 187. Here is line 187:

inputVerificationCode() {
// some code}

Now, when I connect my samsung S4, the app works fine and I can see my main page. My S4 is on Android version 5.0.1 while my Samsung tablet is at version 4.4.4. Does this have any effect on the app? (my tablet says it is currently updated so maybe it does not have an official v5.


#2

It’s quite possible that the browser shipped with Android 5.0.1 is fixing an error that the browser in 4.4.4 won’t. You need to post way more of your code, not just a single line. I’d say even that single line is suspect, because it looks like a function definition but doesn’t contain the word function:

// Potentially scary
inputVerificationCode() {
  // do stuff
}

// Probably good
function inputVerificationCode() {
  // do stuff
}

But I have no way of knowing that for sure, because I have no context for that line of code.


#3

Thanks for responding. Here’s the block of code:

        cognitoUser.forgotPassword({
        onSuccess: function (result) {
            console.log('call result: ' + result);
            alert(result);
        },
        onFailure: function(err) {
            alert(err);
        },
        inputVerificationCode() {
            $scope.verificationCode = prompt('Please input verification code ' ,'');
            cognitoUser.confirmPassword($scope.verificationCode, $scope.resetPassword.newPassword, {
                onSuccess: function (result) {
                    // alert(result);
                    alert('Password Reset');
                    $state.go('login')
                },
                onFailure: function(err) {
                    alert(err);
                }

            });

            }
    });

It’s part of the cognito user pool function.

Here’s the link from AWS for this function as well: http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html#using-amazon-cognito-user-identity-pools-javascript-examples-forgot-password-flow


#4

Okay, perfect, thank you. So now it makes perfect sense why it worked in new Android but broke in older, it’s not an error, but rather the use of a new version of JavaScript. JavaScript ES2015 (named as such because it’s standard was approved in 2015, long after Android 4.4.4) has a new feature called “object method shorthand” that lets you directly declare functions just like you do on that line

// This is okay in ES2015
const testObject = {
  inputVerificationCode() {
    //do something
  }
}

// But in ES5 (the previous version of JavaScript), you must make the property point to a function explicitly, like you are doing everywhere else in your code
const testObject = {
  inputVerificationCode: function() {
    // Do something
  }
}

Based on your code sample you are declaring every other method this way, for example onFailure: function(). So my recommendation would be to simply change the offending line to inputVerificationCode: function() {.

However if you’d like to continue to use modern JavaScript you can use Babel to transpile it back to ES5 so it works on older browsers.


#5

Thanks for this. it looks like it solved the problem!