This may be a simple problem but I can't get my head around it. I have a javascript file that looks like this:
import { canWeCamera } from './modules/_canWeCamera.js';
import { qrReader } from './modules/_qrReader.js';
function init() {
canWeCamera();
// Need to only run the function below if there is a camera
qrReader();
}
init();
And the canWeCamera import looks like this:
export function canWeCamera() {
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
console.log("enumerateDevices() not supported.");
}
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
if(device.kind == 'videoinput') {
alert('There is a video device');
}
});
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});
}
Which is all working as expected.
My next step is only running the qrReader function if there is a camera. I guess I need to return a variable from the canWeCamera function, but I can't get it to work.
If anyone has any pointers that would be great