5 Replies Latest reply on Aug 18, 2014 6:20 AM by Mark Ross

    storeBlob function never called in RhoConnect javascript models

    Mark Ross

      I am having issues trying to sync photos taken with the camera on a mobile phone to RhoConnect.  I am using Rho 4.0 and RhoConnect 4.0 - both using javascript APIs (I'm forced to use the 2.2 javascript APIs as well since I need to access the camera).  On my RhoConnect server, the storeBlob function for the model I'm syncing sometimes is called and sometimes is not called - without any code changes or server restarts.  I cannot figure out how or what is causing the function to be or not be called.

       

      Here are the general steps I take in the app and what I expect in RhoConnect:

       

      1. User takes a photo in the mobile app
      2. Photo is added to a model ("product") which has a property of "blob"
      3. Model is synced
      4. Expect: RhoConnect storeBlob function should be called before create function to store the blob (photo)
      5. Actual: storeBlob function is never or randomly called before create function and does not store the blob
      6. Create function runs and does not have access to the blob (photo)

       

      I've gone ahead and created a test application (mobile and rhoconenct) to see if I can reproduce the same behavior.  In the test app I created, the storeBlob function is never called (so its reproducible, but even worse).  I am attaching the sample app and sample rhoconnect app.  In the rhoconnect app, the query and create functions just call a page which return simple JSON.

       

      Here's my javascript model:

      var Product = Rho.ORM.addModel(function(model) {
        model.modelName('Product');
        model.enable('sync');
        model.property('name', 'string');
        model.property('photo', 'blob');
        model.property('media_url', 'string');
      });
      

       

      Here's my storeBlob function:

      this.storeBlob = function(resp){
        var uploadDir = 'uploads/media/';
      
        if (fs.existsSync(resp.params.path)) {
        if (!fs.existsSync(uploadDir)) {
        fs.mkdirSync(uploadDir);
        }
      
        fs.readFile(resp.params.path, function(err, data) {
        if (err) {
        throw err;
        }
      
        var tempFile = data;
      
        fs.writeFile(uploadDir + resp.params.filename, tempFile, function(err) {
        if (err) {
        throw err;
        }
        resp.send(uploadDir + resp.params.filename);
        });
        });
        } else {
        console.log('Please provide some code to handle blobs if you are using them.');
        resp.send(false);
        }
      };
      

       

      And my create function:

      this.create = function(resp){
        var product = resp.params.create_object;
      
        if (product.photo && product.photo !== '') {
        if (fs.existsSync(product.photo)) {
        var fileData = fs.readFileSync(product.photo, {encoding: 'base64'});
      
        product.file_string = fileData;
        product.filename = product.photo.split('/').pop();
        }
        }
      
        var postData = JSON.stringify(product),
        str = '',
        options = {
        host: 'example.com',
        path: '/path/to/my/service/',
        method: 'POST',
        headers: {
        'Content-Type': 'application/json',
        'Content-Length': postData.length
        }
        };
      
        var req = http.request(options, function(res) {
        res.on('data', function(chunk) {
        str += chunk;
        });
      
        res.on('end', function() {
        var data = JSON.parse(str);
      
        resp.send(data.id.toString());
        });
      
        res.on('error', function(e) {
        console.log('problem with request: ' + e.message);
        });
        });
      
        req.write(postData);
        req.end();
      };
      

       

       

      I'm out of options here.  I've searched and searched for things that I might be missing, but have found little to no documentation on blobs using javascript APIs.  I did find this other question that is essentially asking the same thing and the question has no answer (Ability to sync blobs using javascript ORM model).  Any help or direction is very appreciated.  We are at a stand-still with our enterprise applications without image syncing.

       

      -Mark