jaystack / jaydata

Notice: this library isn't maintained anymore
http://jaydata.org
GNU General Public License v2.0
352 stars 95 forks source link

Cannot catch errors using saveChanges method. #84

Open mohammadchehab opened 11 years ago

mohammadchehab commented 11 years ago

While Saving the first time i get a success with no problems. However when i try to save again primary keys exceptions are cought in the console however i cannot catch them using try catch blocks to raise an error. `javascript offline = new PlannerServiceLibrary.PlannerEntities({ name: 'sqLite', databaseName: 'progressdb' });

            online = new PlannerServiceLibrary.PlannerEntities({
                name: 'oData',
                oDataServiceHost: '/Planner.svc',
                maxDataServiceVersion: '3.0'
            });

            online.onReady(function (online) {

                console.log("ready");

                online.vCrews.toArray(function (crews) {

                     try {
                        offline.onReady(function (offline) {
                            offline.vCrews.addMany(crews);
                            offline.saveChanges(function () {

                                console.log("saved crews");
                            });
                        });

                    } catch (e) {
                        alert("Gotacha!!");
                    }

                });

one more thing i get is when the database is completely new I create the tables and check the database the tables are there and the data are there however i get in the console the following errors.

DefaultError: DEFAULT ERROR CALLBACK!

Exception {name: "DefaultError", message: "DEFAULT ERROR CALLBACK!", data: Arguments[1], _getStackTrace: function}
data: Arguments[1]
0: SQLError
code: 5
message: "could not prepare statement (1 no such table: vCrews)"
agdolla commented 11 years ago

Use saveChanges the promise way: saveChanges() .then(function(){ handle success here }) .fail(function(reason){ handle error here });

mohammadchehab commented 11 years ago
 online.onReady(function (online) {

                    console.log("ready");

                    online.vCrews.toArray(function ( crews ) {

                        offline.onReady(function (offline) {

                            offline.vCrews.addMany(crews);
                            var promise = offline.saveChanges();
                            console.log(promise);
                            promise
                            .done(function () {
                                console.log("done");
                            })
                       });
                    });
                });

Result

Promise {always: function, done: function, fail: function, isRejected: function, isResolved: function…}
 index.test.html:58
Not implemented!: $data.Promise.done

Exception {name: "Not implemented!", message: "$data.Promise.done", data: undefined, _getStackTrace: function}
 jaydata.js:87
Uncaught Not implemented!: $data.Promise.done jaydata.js:91
DefaultError: DEFAULT ERROR CALLBACK!

Exception {name: "DefaultError", message: "DEFAULT ERROR CALLBACK!", data: Arguments[1], _getStackTrace: function}
 jaydata.js:87
Uncaught DefaultError: DEFAULT ERROR CALLBACK! jaydata.js:91
agdolla commented 11 years ago

promise .then() .fail()

agdolla commented 11 years ago

you also need a promise library if you already use jQuery then that is enough otherwise use q promise

mohammadchehab commented 11 years ago

Am using jQuery and this is what i have tried please tell me where am i wrong

                online.onReady(function (online) {

                    online.vCrews.toArray(function (crews) {

                        offline.onReady(function (offline) {

                            offline.vCrews.addMany(crews);
                            var promise = offline.saveChanges();
                            console.log(promise);
                            promise
                            .then(function () {
                                console.log("then");
                            })
                            .fail(function () {
                                console.log("fail");
                            })
                        });
                    });
                });

result

Promise {always: function, done: function, fail: function, isRejected: function, isResolved: function…} index.test.html:61 Not implemented!: $data.Promise.then

Exception {name: "Not implemented!", message: "$data.Promise.then", data: undefined, _getStackTrace: function} jaydata.js:87 Uncaught Not implemented!: $data.Promise.then jaydata.js:91 DefaultError: DEFAULT ERROR CALLBACK!

Exception {name: "DefaultError", message: "DEFAULT ERROR CALLBACK!", data: Arguments[1], _getStackTrace: function} jaydata.js:87 Uncaught DefaultError: DEFAULT ERROR CALLBACK!

The funny thing is when am quering data from a store this works like a charm however in here its not working am missing something just dont know what is it yet.

agdolla commented 11 years ago

You have to include jaydatamodules/deferred.js

mohammadchehab commented 11 years ago

@agdolla you are a genius. Thank YOU !!

agdolla commented 11 years ago

Actually, you should not include deferred.js or qDeferred.js manually, it should work automagically. The reason I think why it does not work for you only with manual include is that you include jquery after jaydata. Could you please include jquery before jaydata and remove the manual inclusion of deferred and try it ?

mohammadchehab commented 11 years ago

any problems with this ? it is working just fine

mohammadchehab commented 11 years ago

not at all am including jquery before jayData here is the code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Progress Everywhere</title>
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <link rel="stylesheet" type="text/css" href="assets/jquerymobile/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" type="text/css" href="assets/custom.css" />
    <script type="text/javascript" src="assets/jquery/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="assets/jquerymobile/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" src="config.js"></script>
    <script type="text/javascript" src="assets/datajs/datajs-1.0.3.js"></script>
    <script type="text/javascript" src="assets/jaydata/jaydata.js"></script>
    <script type="text/javascript" src="assets/jaydata/jaydatamodules/deferred.js"></script>
    <script type="text/javascript" src="assets/jaydata/jaydataproviders/oDataProvider.js"></script>
    <script type="text/javascript" src="core/PlannerEntities/PlannerEntities.js"></script>
    <script type="text/javascript" src="core/monitor/monitor.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="assets/knockout/knockout-2.2.0.js"></script>
    <script type="text/javascript" src="messages/messages.js"></script>
    <script type="text/javascript">
agdolla commented 11 years ago

I'm glad that it works for you but on the other hand it should work without manual include. Let us leave this issue open so we do not forget to investigate. Which version of jaydata do you use ?

mohammadchehab commented 11 years ago

the latest 1.2.6 (taken from jayData.js "// JayData 1.2.6")

morganhein commented 11 years ago

Is this still being looked into? I have the same issue, however I am using the q library (not including jQuery).

I am using angular as well, which has it's own q/promise implementation, not sure if that is causing an issue?

mohammadchehab commented 11 years ago

can you post some code?

morganhein commented 11 years ago

including scripts:

    <!-- build:js scripts/scripts.js -->
    <script src="scripts/app.js"></script>
    <script type="text/javascript" src="components/q/q.min.js"></script>
    <script type="text/javascript" src="components/jaydata/jaydata.min.js"></script>
    <script type="text/javascript" src="components/jaydata/jaydatamodules/deferred.min.js"></script>
    <script type="text/javascript" src="scripts/factories/storage.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/controllers/header.js"></script>
    <!-- endbuild -->

test code:

var promise = $sa.db.UserInfo.toArray();
promise.then(testCallback);

function testCallback(data) {
    data.forEach(
        function (entity) {
            console.log(entity);
        }
    );
}

This results in:

Not implemented!: $data.Promise.then

Exception {name: "Not implemented!", message: "$data.Promise.then", data: undefined, _getStackTrace: function}

I know the database works because if I don't use promises the data is returned.

mohammadchehab commented 11 years ago

well basically i have used q library and i found that the same thing you can do with jQuery's deferred objects. what you can do is the following:

var done = function(){ console.log("yay"); }; var error = function(e){ console.log("woops " + e.message); } online.Friends.toArray().then(function(its){ local.Friends.addMany(its); var promise = db.saveChanges(); promise.fail(error); promise.done(done); });

mohammadchehab commented 11 years ago

if that doesn't help and you still want to work with Q i belive that jayData has support for q.js instead of adding deferred.js add this file jaydatamodules/qDeferred.js

morganhein commented 11 years ago

Including qDeferred worked. Thank you! Took me nearly 3 hours to figure that out.

mohammadchehab commented 11 years ago

Cheerz!!