Capacitor: TypeError: cameraModal.componentOnReady is not a function (on Android emulator)

Created on 27 Aug 2018  路  5Comments  路  Source: ionic-team/capacitor

Tried using the camera feature on an Android emulator:

const image = await Camera.getPhoto({ quality: 98, allowEditing: false, resultType: CameraResultType.Base64, source: CameraSource.Camera });

TypeError: cameraModal.componentOnReady is not a function at CameraPluginWeb.<anonymous> (https://localhost:6147/build/0.js:654:70) at step (https://localhost:6147/build/vendor.js:19296:23) at Object.next (https://localhost:6147/build/vendor.js:19277:53) at https://localhost:6147/build/vendor.js:19270:71 at new t (https://localhost:6147/build/polyfills.js:3:21506) at Object.__awaiter [as b] (https://localhost:6147/build/vendor.js:19266:12) at https://localhost:6147/build/0.js:646:143 at new t (https://localhost:6147/build/polyfills.js:3:21506) at CameraPluginWeb.<anonymous> (https://localhost:6147/build/0.js:646:39) at step (https://localhost:6147/build/vendor.js:19296:23)

Most helpful comment

Add

in your index.html file

All 5 comments

Solved. I had to correct my imports.

From:
import { Camera, CameraResultType, CameraSource } from '@capacitor/core';

To:

import { Plugins, CameraResultType, CameraSource } from '@capacitor/core';
const { Camera } = Plugins;

Add

in your index.html file

Note: the solution above doesn't work for offline scenarios

If you are not using Angular or React, it's easy to overlook the fact that defineCustomElements(window) returns a promise. Thus you can still get the error mentioned by @JVanloofsvelt unless you put the rest of the camera code in the then(). Here's one way to get it right, in TypeScript:

function takePicture() {
    let f = () => {
        const { Camera } = Plugins;
        Camera.getPhoto({
            quality: 90,
            allowEditing: true,
            resultType: CameraResultType.Uri
        }).then((image: CameraPhoto) => {
            console.log(image);
        }).catch((reason: any) => {
            console.log(reason);
        });
    }
    if (!init) {
        init = true;
        defineCustomElements(window).then(() => f());
    }
    else {
        f();
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bogdbo picture bogdbo  路  3Comments

gnesher picture gnesher  路  3Comments

daniel-lucas-silva picture daniel-lucas-silva  路  3Comments

ebk46 picture ebk46  路  3Comments

nicobytes picture nicobytes  路  3Comments