Ionic 4 - Ionic wrapper for custom plugins Error: Object(...) is not a function

I built a plugin then i create his wrapper for work with typescript, this code works on ionic 3 but i have this error on ionic 4

this is my ionic native wrapper for my custom plugin

  1. file-path: src/@ionic-native/plugins/signal-strenght/index.ts
import { Injectable } from '@angular/core';
import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core';

@Plugin({
    pluginName: 'SignalStrength',
    plugin: 'cordova-aspolar-plugin-signal-strength',
    pluginRef: 'window.plugins.signalStrength',
    repo: 'https://github.com/JVasconsueloM/aspolar-signal-strength',
    platforms: ['Android']
})
@Injectable()
export class SignalStrength extends IonicNativePlugin {

    @Cordova()
    getdBm(): Promise<any> { return; }

    @Cordova()
    getPercentage(): Promise<any> { return; }

    @Cordova()
    getLevel(): Promise<any> { return; }
}

  1. on app.module.ts
    Screenshot_3

  2. I get this error

Hi @AsPolar :wave:

For Ionic 4 you need to use Ionic Native 5 and add /ngx at the end of the path for all @ionic-native imports.

Usage: https://github.com/ionic-team/ionic-native/blob/master/README.md#ionicangular-apps

Best,
Rodrigo

thanks romero. but this is a cordova custom plugin it hasn’t a typescript file on @ionic-native that’s why i need build a wrapper for that plugin.

Sorry, I misread your message and didn’t notice this is a custom plugin :sweat_smile:

1 Like

Try to write the provider similar to the compiled Ionic Native code

import { Injectable } from '@angular/core';
import { cordova, IonicNativePlugin } from '@ionic-native/core';

@Injectable()
export class SignalStrength extends IonicNativePlugin {
    static pluginName = 'SignalStrength',
    static plugin = 'cordova-aspolar-plugin-signal-strength',
    static pluginRef = 'window.plugins.signalStrength',
    static repo = 'https://github.com/JVasconsueloM/aspolar-signal-strength',
    static platforms = ['Android']

    getdBm(): Promise<any> { 
       return cordova(this, 'getdBm', {}, []};
    }

}
3 Likes

did you find the solution? I am faced with the same problem :confused:

1 Like

Hi!!, can you share the solution pls :frowning: because I tried @jean-pierre-ilofo-sy solution but it returns “invalid action”

1 Like

I need too :grinning:

Hi Did you get solution for this ?
if you get the solution ,please share here .
thanks in advance

Hi, I am also looking for solution how to use custom cordova plugin from ionic react app using ionic 4?

I’m going to start using Capacitor, because it’s too easy to create plugins with it. In addition it is plug and play with ionic 4.

did anyone found the solution, as I am getting the same error

Hi,

The reply from jean-pierre-ilofo-sy (Ionic 4 - Ionic wrapper for custom plugins Error: Object(...) is not a function) worked for me.

The issue was with the @Cordova decorator.

@goku-dev
@ggalvez92
@blondie63
@venkibendi
@sandhyasuman
@ankitmrright
sorry to answer later.

for all ionic version (3, 4, 5) you can declare cordova and use it directly;

example:

import { Injectable } from '@angular/core';

/*** ADD THIS ***/
declare let cordova: any;

@Injectable({
    providedIn: 'root'
})
export class GeolocationGoogleService {

    constructor() {
    }

    getCurrentPositionNative( options ): Promise<any> {
        return new Promise(( resolve, reject ) => {
             /*** AND ADD THIS ***/
            cordova.plugins.locationServices.geolocation.getCurrentPosition(( e ) => resolve(e), ( e ) => reject(e));
        });
    }

}

you can log cordova for know exactly plugin location in cordova object.

Thanks for yours reply.