Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

cordova - PushPlugin TypeError: Object #<Object> has no method ''exec

Background

  1. I installed PushPlugin. According to the docs I used automatic installation. But when I run cordova run android, JavaScript returns the error, 'Cannot read property pushNotification of undefined'

  2. If I add

    <script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>
    

    then the error changes to the one in this question's title.

  3. This is how my HTML loads the scripts

    <script type="text/javascript" src="cordova.js"></script>
    
    <script src="js/libs/jquery-1.10.2.js"></script>
    <script src="js/libs/handlebars-1.1.2.js"></script>
    <script src="js/libs/ember-1.5.1.js"></script>
    
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/model.js"></script>
    <script type="text/javascript" src="js/router.js"></script>
    <script type="text/javascript" src="js/controller.js"></script>
    <script type="text/javascript" src="js/view.js"></script>
    

    Initialization code is in index.js where after deviceready I call pushNotification.register.

    After the register completes, I call MyEmberApp.deferReadiness()

  4. After automatically installing the plugin, I just have to run register, according to the docs. But this still leads to 'Cannot read pushNotification....'

  5. It seems that PushNotification.js is automatically inserted after deviceready fires. But the plugin is not doing so. If I insert the script in index.html, the error Object has no method 'exec' occurs because deviceready hasn't fired yet.

  6. deviceready

    if ('device is android') {
        document.addEventListener("deviceready", this.onDeviceReady(), false);
    }
    

Question

What am I doing wrong? How should I do this?

Update

I just realized that I have only tried the automatic installation. I have not tried the manual steps. But that is no reason why the direct plugin install shouldn't work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally realized that the error was due to the EventListener for deviceready. I changed

  if ('device is android') {
        document.addEventListener("deviceready", this.onDeviceReady(), false);
  }

to

  document.addEventListener("deviceready", this.onDeviceReady, false);

and everything fell right into place. Though this is a careless mistake, I still leave this question and it's answers for others who might encounter this issue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...