问题
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:
Meteor create new project
Added the cordova camera package
meteor add cordova:org.apache.cordova.camera@0.3.1
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