Meteor Cordova: Cannot get camera plugin to work, Uncaught TypeError: Cannot read property 'getPicture' of undefined

旧巷老猫 提交于 2020-01-06 19:39:28

问题


I am trying out the Meteor Cordova camera plugin and it won't work, and I have no idea what I'm missing.

Here is what I did:

  1. Meteor create new project

  2. Added the cordova camera package meteor add cordova:org.apache.cordova.camera@0.3.1

  3. Programmed the default button on screen to take a picture and then append it to the body.
    This should use the camera built into the laptop computer but all I get when I click the button is: Uncaught TypeError: Cannot read property 'getPicture' of undefined

    And when I run the app in the iPhone simulator with the command meteor run ios and click the button I get a popup error: Failed because: no camera available

This is what my html and js file look like:

main.html:

<head>
  <title>cameraTest</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}
</body>

<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>

main.js:

if (Meteor.isClient) {
  // counter starts at 0

  function onSuccess(imageData) {
      var image = document.getElementById('myImage');
      image.src = "data:image/jpeg;base64," + imageData;
      $('body').append(image);
  }

  function onFail(message) {
      alert('Failed because: ' + message);
  }

  Session.setDefault("counter", 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get("counter");
    }
  });

  Template.hello.events({
    'click button': function () {

      navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
        destinationType: Camera.DestinationType.DATA_URL
      });
    }
  });
}

回答1:


This is because cordova code must be included into a closure, as said on documentation. You have to do this in your events functions. See bottom.

PS: you can not test camera on iOS emulator, use a real iPhone instead.

Meteor.startup(function () {
    // The correct way
    navigator.geolocation.getCurrentPosition(success));
});

// Will not work
navigator.geolocation.getCurrentPosition(success));

Your code will become:

Template.hello.events({
  'click button': function () {
    Meteor.startup(function() {
      navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
        destinationType: Camera.DestinationType.DATA_URL
      });
    });
  }
});


来源:https://stackoverflow.com/questions/26454473/meteor-cordova-cannot-get-camera-plugin-to-work-uncaught-typeerror-cannot-rea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!