0

I am trying to load the pouchdb client database on start of the application from within a javascript file but the database does not get created. There are no console errors in the debugger. When I try to debug, it seems to fail on this line below because it exits the anonymous function and does not allow me to step into the function further.

fails here--> angular.module('app').factory(serviceId, [ 'pouchdb', createclientdb]);

Any idea why I may be doing wrong?

 (function () {
        'use strict';

        var serviceId = 'clientedb';
        angular.module('app').factory(serviceId, [ 'pouchdb', createclientdb]);

        function createclientdb(pouchdb) {

            return pouchdb.create('mydb', function callback(err, result) {

                if (!err) {
                    alert('Successfully added a program!');
                }
                else {
                    alert('Failed added a program!');
                }
            });

            return true;
        }

    });

Latest error generate uncaught exception.

 (function () {

        'use strict';

        var databasename = 'mydb';

        angular.module('app.clientdb', [])
            .factory('clientdb', ['pouchdb', function (pouchdb) {
                return pouchdb.create(databasename, function callback(err, result) {

                    if (!err) {
                        alert('Successfully added a program!');
                    }
                    else {
                        alert('Failed added a program!');
                    }
                });
            }]);

    });

I also have the following service. Any suggestions?

(function () {
    'use strict';

    angular.module('app').factory('clientdbservice', ['pouchdb','app.clientdb',clientdbservice]);


    function clientdbservice(clientdb) {
        return {
           adddata: function (obj) {
               clientdb.put(obj)
                    .then(function () {
                     log('data added to  Client DB');
                    })
                    .catch(function (error) {
                        log('Could not add data to  Client DB');
                    }).finally(function () {
                        // Do something when everything is done
                    });
            }
               };
        };
});
Nate
  • 2,044
  • 4
  • 23
  • 47

1 Answers1

0

I'm not 100% where exactly your error is, but the following is working for me

'use strict';
angular.module('app.clientedb', [])
    .factory('clientedb', ['pouchdb', function(pouchdb) {
        return pouchdb.create('mydb', function callback(err, result) {

            if (!err) {
                alert('Successfully added a program!');
            }
            else {
                alert('Failed added a program!');
            }
        });
    }]);

I'm also including my js files in order

<script src="/lib/pouchdb-nightly.js"></script>
<script src="/lib/angular-pouchdb.js"></script>

And I'm setting up angular lke

angular.module('app', [
                       'pouchdb',
                       'app.clientedb',
])
jrhicks
  • 14,759
  • 9
  • 42
  • 57
  • I am getting uncaught exception. I will update main question. – Nate May 28 '14 at 18:43
  • "[$injector:modulerr] Failed to instantiate module app.clientedb due to: Error: [$injector:nomod] Module 'app.clientedb' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify – Nate May 28 '14 at 20:37
  • you may have called your clientdbservice instead of app.clientedb – jrhicks May 28 '14 at 21:11
  • I had it named wrong. But now it no longer raises any error but does not create a DB. No exception is raised. I would think that something is missing or commented out for this but I have not found that this is the case so far. – Nate May 28 '14 at 21:26
  • Can you please send me a copy of your test file that you ran to make this work? Thanks – Nate May 28 '14 at 21:59
  • Just pushed my repository to - https://github.com/jrhicks/ionic-pouchdb-base - take a look at app/app.js, app/components/services/db_factory.js – jrhicks May 28 '14 at 22:22