流星科尔多瓦:无法使相机插件正常工作,未捕获的TypeError:无法读取未定义的属性“ getPicture” [英] Meteor Cordova: Cannot get camera plugin to work, Uncaught TypeError: Cannot read property 'getPicture' of undefined

查看:233
本文介绍了流星科尔多瓦:无法使相机插件正常工作,未捕获的TypeError:无法读取未定义的属性“ getPicture”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用Meteor Cordova相机插件,该插件无法正常工作,我也不知道自己缺少什么。

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

这就是我所做的:


  1. 流星创建新项目

  1. Meteor create new project

添加了cordova相机包 meteor add cordova:org.apache.cordova.camera@0.3.1

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

对屏幕上的默认按钮进行编程以拍摄图片,然后将其附加到主体上。

应该使用内置的相机进入便携式计算机,但是当我单击按钮时,我得到的只是: Uncaught TypeError:无法读取未定义的属性'getPicture'

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

然后,当我使用命令 meteor run ios 在iPhone模拟器中运行该应用程序并单击按钮时,出现弹出错误: Failed因为:没有可用的相机

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

这是我的html和js文件的样子:

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
      });
    }
  });
}


推荐答案

这是因为cordova代码必须如文档。您必须在事件函数中执行此操作。见底部。

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:您无法在iOS模拟器上测试相机,请使用真实的iPhone。

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));

您的代码将变为:

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

这篇关于流星科尔多瓦:无法使相机插件正常工作,未捕获的TypeError:无法读取未定义的属性“ getPicture”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆