0

I'm trying to implement a Camera Application with Ionic Framework and Cordova and struggling to understand why it's throwing out the following errors and thus unable to instantiate the camera:

      0     444468   error    Uncaught SyntaxError: Unexpected token ., http://192.168.0.10:8100/js/app.js, Line: 9
      1     444540   error    Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
      Error: [$injector:nomod] Module 'starter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

The following question and answers unfortunately didn't help me in answering my question:

Ionic Framework / Cordova Camera

Cordova Camera - Ionic

navigator.camera.getPicture doesn’t call success callback - ionic framework - cordova

I have referred to the following tutorials to get this job done with no success(sometimes I got confused more):

https://www.youtube.com/watch?v=1KItyjeqhx0

https://github.com/driftyco/ionic-example-cordova-camera

http://learn.ionicframework.com/formulas/cordova-camera/

index.html

  <!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
 </head>
 <body ng-app="starter">
<ion-pane>
  <ion-header-bar class="bar-assertive">
    <h1 class="title">Title of App</h1>
  </ion-header-bar>
  <ion-content ng-controller="CameraCtrl">
    <div class="list card"
    <div class="item item-image">
      <img ng-src="{{pictureUrl}}">
    </div>
  </div>
  <div class="padding">
    <button ng-click="takePicture()"
    class="button button-block button-assertive">Take a Photo</button>
  </div>
  </ion-content>
</ion-pane>
 </body>
 </html>

app.js

    // Ionic Starter App

    angular.module('starter', ['ionic', 'ngCordova']);

  .controller('CameraCtrl', function($scope, $cordovaCamera) {

   $scope.pictureUrl = 'http://placehold.it/300x300';

   $scope.takePicture = function() {
   $cordovaCamera.getPicture({})
      .then(function(data) {
       console.log('camera data': + angular.toJson(data));
      }, function(error) {
        console.error('camera error': + angular.toJson(data));
      });
    }

  )};

  .run(function($ionicPlatform) {
   $ionicPlatform.ready(function() {
     if(window.cordova && window.cordova.plugins.Keyboard) {
       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
     }
     if(window.StatusBar) {
       StatusBar.styleDefault();
     } 
   });
 })

Thank you in advance for all your help!

Community
  • 1
  • 1
stefbmt
  • 111
  • 1
  • 2
  • 14

2 Answers2

3

Jad Salhani is right.

I am going to answer here, even if this should be a comment, as I don't have enough space there.

You have 2 options here. You can define a variable you are going to use in your module (app.js):

var app = angular.module('starter', ['ionic', 'ngCordova']);

and the use the app variable:

app.run(function($ionicPlatform) {
   $ionicPlatform.ready(function() {
     if(window.cordova && window.cordova.plugins.Keyboard) {
       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
     }
     if(window.StatusBar) {
       StatusBar.styleDefault();
     } 
   });
 });

app.controller('CameraCtrl', function($scope, $cordovaCamera) {
   ...
});

or remove the semi columns:

angular.module('starter', ['ionic', 'ngCordova'])

.run(function($ionicPlatform) {
       $ionicPlatform.ready(function() {
         if(window.cordova && window.cordova.plugins.Keyboard) {
           cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
         }
         if(window.StatusBar) {
           StatusBar.styleDefault();
         } 
       });
     })

.controller('CameraCtrl', function($scope, $cordovaCamera) {
       ...
});
Community
  • 1
  • 1
LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • 100% Correct, I missed the semi-colon at the end of the .controller declaration that's why an other error showed! Great clarification – Jad Salhani Aug 05 '15 at 08:47
  • @JadSalhani: Yes, mine was just a clarification. Your answer is the good one, though :-) – LeftyX Aug 05 '15 at 08:48
2
   angular.module('starter', ['ionic', 'ngCordova']);

This is where your error lies, there shouldn't be a semi-colon there.

its angular.module('moduleName').controller('controllerName', function(){});

Jad Salhani
  • 480
  • 1
  • 3
  • 12
  • Despite your suggested fix, now having the following errors `0 830977 error Uncaught SyntaxError: Unexpected token :, http://192.168.0.10:8100/js/app.js, Line: 16 1 831058 error Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to: Error: [$injector:nomod] Module 'starter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.` – stefbmt Aug 05 '15 at 08:07
  • The code you asked me to fix looks like this now `angular.module('starter' ['ionic', 'ngCordova']).controller('CameraCtrl', function($scope, $cordovaCamera)` – stefbmt Aug 05 '15 at 08:08
  • @stefbmt Check the answer below, I missed the semi-colon at the end of the .controller declaration that's why you're still getting this error. Sorry for the trouble! – Jad Salhani Aug 05 '15 at 08:45