9 Replies Latest reply on Nov 13, 2013 7:08 AM by Tom Mann

    Issue with Javascript fixedSchema and find with conditions

      When the schema is property bag, I can find an object with the following code

      var model = Rho.ORM.getModel("Trip");

      var trip = model.find("first", {conditions: { "object": id.toString() }});

       

      When I changed the schema to fixed schema the above code gives the error

      TypeError: Result of expression 'cond['$is_a?']' [undefined] is not a function in the rhoapi-modules.js on line 7092

       

      I was able to find the object by looping over all the records as in the code below but this is very inefficient.

       

      var model = Rho.ORM.getModel("Trip");

      var trips = model.find("all");

      for (var i = 0; i < trips.length; i++) {

          var trip = trips[i];

                 

          if (trip.get("object") === id.toString()){

              console.log(trip.get("employee_id"));

          };

      }

             

      It seems no matter what kind of condition I use I get the error when using fixedSchema. How do I resolve?

        • Re: Issue with Javascript fixedSchema and find with conditions
          BHAKTA RANJAN SATAPATHY

          Have you added jquery ??

           

          Kindly take a look on this thread https://developer.motorolasolutions.com/thread/4200 .

           

          Thanks

          Bhakta

          • Re: Issue with Javascript fixedSchema and find with conditions

            For finding object in model need get it by id

             

            var trip = model.find(id);

             

            or

             

            var trip = model.find(id.toString());

              • Re: Issue with Javascript fixedSchema and find with conditions

                When trying this I now get this error

                ReferenceError: Can't find variable: string_from_key_vals in rhoapi_modules.js on line 1484

                 

                According to the documentation I have been able to find, model.find(id) is not even listed. http://docs.rhomobile.com/api/OrmModel#mfind

                 

                Can you share a simple example of this that is using just javascript, fixed schema, and find? I must be missing some setting or some magical piece of code. I have gotten this working when using propertyBag for my models but have had nothing but errors when switching my models to fixedSchema. I would have thought the find would work the same for both types of schemas.

              • Re: Issue with Javascript fixedSchema and find with conditions

                This is the code ,as simple as I could make it, I used to demonstrate the errors I am seeing. I created the applicaton in RhoStudio by clicking on File / New / RhoMobileApplication. I then added the test.html file, that is represented below, in the public folder. I changed the RhoConfig.txt file start path to start_path = '/public/test.html'

                 

                <!DOCTYPE html>

                <html>

                <head>

                <meta charset="ISO-8859-1">

                <title>Insert title here</title>

                    <script src="/public/api/rhoapi-modules.js" type="text/javascript"></script>

                    <script src="/public/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>

                </head>

                <body>

                 

                 

                <script type="text/javascript">

                //create ORM model

                var userModel = Rho.ORM.addModel(function(model) {

                    model.modelName('User');

                    model.property('name','string');

                    model.property('email','string');

                    // optionally enable sync for rhoconnect applications

                    // model.enable('sync');

                    // optionally, define the model as fixed schema default is propertyBag

                    //model.enable('fixedSchema');

                });

                 

                //determine if there are existing records if not add some

                var model = Rho.ORM.getModel('User');

                var users = model.find('all')

                console.log("existing records:" + users.length);

                if (users.length == 0) {

                    console.log("adding 3 records");

                    var user = userModel.create({

                            name: 'Alice',

                            email: 'alice@example.com'});

                           

                    var user = userModel.create({

                            name: 'John',

                            email: 'john@example.com'});

                           

                    var user = userModel.create({

                            name: 'Zack',

                            email: 'zack@example.com'});

                }

                 

                //grab the ids of the records

                var users = model.find('all')

                var ids = new Array();

                for (var i = 0; i < users.length; i++) {

                    var user = users[i];

                    ids[i] = user.get("object");

                    console.log(ids[i]);

                }

                 

                //do a search to find each object associated with the ids

                console.log("ids to search: " + ids.length);

                for (var i = 0; i < ids.length; i++) {

                   

                    var user = model.find(

                        'first',

                        {

                            conditions: {object: ids[i].toString()}

                        }

                    );

                    console.log(user.get("name"));

                }

                </script>

                 

                 

                </body>

                </html>

                 

                 

                console.log output:

                 

                existing records:3

                153499326215000.9

                153499326215001.9

                153499326215002.9

                ids to search: 3

                Alice

                John

                Zack

                 

                This code works perfect but the model is in the default of propertyBag. I require the model to be in fixedSchema for my project.

                 

                Delete the rhosimulator folder from my hard drive and uncomment this line to enable fixedSchema

                model.enable('fixedSchema');

                 

                Resulting Error:

                TypeError: Result of expression '$' [undefined] is not an object. rhoapi-modules.js on line 1276

                 

                 

                Delete the rhosimulator folder from my hard drive and change order of javascript libraries. This allows you to create the model as fixed schema but causes an error in the find with conditions.

                 

                <script src="/public/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>

                <script src="/public/api/rhoapi-modules.js" type="text/javascript"></script>

                 

                Resulting Error:

                existing records:3

                153499326215000.9

                153499326215001.9

                153499326215002.9

                ids to search: 3

                TypeError: Result of expression 'cond['$is_a?']' [undefined] is not a function. rhoApi-modules.js on line 7092

                 

                All testing was done in the RhoSimulator of RhoStudio 4.0. All I can conclude is that this is a bug.

                • Re: Issue with Javascript fixedSchema and find with conditions
                  Robert Galvin

                  Tom

                   

                  Engineering has reproduced the issue and it seems like it is a bug using FixedSchema and find. Have you suggested the find by id mentioned by someone else? I will see if there are other work arounds.

                   

                  Rob

                    • Re: Issue with Javascript fixedSchema and find with conditions

                      I have tried this to see if it would work but am getting the following error ReferenceError: Can't find variable: string_from_key_vals rhoapi-modules.js on line 1484

                       

                      I replaced the code

                      for (var i = 0; i < ids.length; i++) {

                         

                          var user = model.find(

                              'first',

                              {

                                  conditions: {object: ids[i].toString()}

                              }

                          );

                          console.log(user.get("name"));

                      }

                       

                      with

                       

                      for (var i = 0; i < ids.length; i++) {

                         

                          var user = model.find(ids[i].toString());

                           console.log(user.get("name"));

                      }

                       

                      According to the documentation I have been able to find, model.find(id) is not even listed. http://docs.rhomobile.com/api/OrmModel#mfind . Is this an undocumented option?