Meteor-CollectionFS icon indicating copy to clipboard operation
Meteor-CollectionFS copied to clipboard

Cordova - No Uploads - SYNTAX_ERR: DOM Exception 12 [undefined]

Open ppryde opened this issue 11 years ago • 18 comments

I am working on a mobile first project and am very keen to use CollectionFS in all it glory alongside Cordova, unfortunately I am receiving the below output when trying to upload from Cordova but not from web app. Running meteor 1.0.2.1 (but have reverted back as far as 1.0.1 debugging) with latest release of cfs:filesystem/cfs:standard-packages:. I have created a repo for this and have tested on both local vm and remote host and encountering the same issues when running cordova. I only ever get an issue when trying to upload, I can view the images inserted through the browser fine although they need to have root_url prefixed to the urls...

REPO: https://github.com/ppryde/cfs_test

This may be a replica of issue https://github.com/CollectionFS/Meteor-CollectionFS/issues/463 although this should now be fixed? so I dont think it is the same issue

I20150103-11:43:45.963(-5) (android:http://meteor.local/packages/cfs_upload-http.js:467) HTTP uploadFile
I20150103-11:43:45.969(-5) (android:http://meteor.local/packages/cfs_file.js:273) GET FILERECORD: vhC6b83QajdeXRcaD
I20150103-11:43:45.969(-5) (android:http://meteor.local/packages/cfs_file.js:298) UPDATE: {"$set":{"chunkSize":2097152,"chunkCount":0,"chunkSum":1}}
I20150103-11:43:45.969(-5) (android:http://meteor.local/packages/cfs_upload-http.js:362) uploading chunk 0, bytes 0 to 29958 of 29958
I20150103-11:43:46.087(-5) (android:http://meteor.local/packages/cfs_file.js:273) GET FILERECORD: vhC6b83QajdeXRcaD
I20150103-11:43:46.286(-5) (android:http://meteor.local/packages/cfs_upload-http.js:368) PUT to URL
I20150103-11:43:46.292(-5) (android:http://meteor.local/packages/cfs_power-queue.js:522) Error: "Queue" SYNTAX_ERR: DOM Exception 12 [undefined], Error: SYNTAX_ERR: DOM Exception 12 [undefined]
I20150103-11:43:46.292(-5)? at http://meteor.local/packages/cfs_upload-http.js?cf37b2941c8cdbfd85cdc3948f57fed4bb429081:379:16
I20150103-11:43:46.293(-5)? at http://meteor.local/packages/cfs_upload-http.js?cf37b2941c8cdbfd85cdc3948f57fed4bb429081:105:7
I20150103-11:43:46.293(-5)? at http://meteor.local/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:801:19
I20150103-11:43:46.293(-5)? at httpCall (http://meteor.local/packages/cfs_upload-http.js?cf37b2941c8cdbfd85cdc3948f57fed4bb429081:237:5)
I20150103-11:43:46.294(-5)? at gotBinaryCallback (http://meteor.local/packages/cfs_upload-http.js?cf37b2941c8cdbfd85cdc3948f57fed4bb429081:370:7)
I20150103-11:43:46.294(-5)? at FileReader.reader.onload (http://meteor.local/packages/cfs_data-man.js?a69f9a3bd18f305c48ef0bb42bbbbe2a047e4d4e:333:7)

packages installed:

autopublish            1.0.2  Publish the entire database to all clients
cfs:filesystem         0.1.1  Filesystem storage adapter for CollectionFS
cfs:standard-packages  0.5.3  Filesystem for Meteor, collectionFS
insecure               1.0.2  Allow all database writes by default
meteor-platform        1.2.1  Include a standard set of Meteor packages in yo...

Running in cordova on an android (SG3) using

meteor run android-device

I have also tried setting the FS.HTTP.baseUrl prior to uploading to see if this would make a difference but it doesn't.

I am a bit stuck and have had two very unproductive days trying various combinations of packages, versions, urls etc! any help would be awesome, even if it just for someone to try the repo and confirm that it doesn't work for them so I can tell if it is just some environmental issue I have got going on, or a couple of pointers for debugging collectionFS as I am a little new to meteor/collectionFS still... Going to dive in tomorrow and try to fork the packages and push in some de-bugging to try and solve it but thought I could put this up in case someone has had a similar issue and it is either known bug or possibly (hopefully - and apologies if this is the case) I am missing something simples?

ppryde avatar Jan 03 '15 17:01 ppryde

I'm wondering if you made any progress @ppryde ?

tekknikk avatar Jan 27 '15 13:01 tekknikk

what api version of android?

raix avatar Jan 27 '15 13:01 raix

@raix it is running Android 4.3 JELLY_BEAN_MR2 on a GT-I9305, is there any more information that would be helpful here? I have a couple of other devices running possibly differing versions of Android, I can give them a shot tomorrow and confirm if I am having this issue across other api versions, although I am pretty sure I tried a few devices when I first encountered this and it prevailed across them all. Much appreciated for even opening this topic and taking a look by the way I know you are a very busy man! :)

@tekknikk Unfortunately not, I had to put it on the later pile for the time being although am about at a stage to start looking at it again but was almost leaning in favour of getting it up and running on cfs-s3 as a workaround in the meantime and coming back to it. Have you run into a similar problem? or is it still just me :(

ppryde avatar Jan 28 '15 01:01 ppryde

When looking at it: https://github.com/CollectionFS/Meteor-cfs-upload-http/blob/master/upload-http-client.js#L25 failes?

FS.debug && console.log('PUT to URL', task.url, task.urlParams);

Prints:

I20150103-11:43:46.286(-5) (android:http://meteor.local/packages/cfs_upload-http.js:368) PUT to URL

so it seems task.url and task.urlParams are not set?

The above would be strange - I'm wondering about the DOM error...

raix avatar Jan 28 '15 07:01 raix

Just checked it isnt the next chunk or image being chunked specifically causing it with a few Kb image and doesn't seem to make a difference. Did you manage to reproduce this @raix? Checked across a couple of other devices (4.2 and 4.4) both have the same issue as 4.3 also took a new VM and fresh install of meteor and encountered the same issue :(

ppryde avatar Jan 28 '15 12:01 ppryde

is Uint8Array supported?

raix avatar Jan 28 '15 14:01 raix

Hi @raix it looks like it should be but may need some formatting tweaks to enable it to work correctly, what package will be using the Uint8array? I can fork and try it out see if thats the case? Heres the link I found in relation to it... http://stackoverflow.com/questions/23702145/convert-base64-to-blob-in-phonegap

ppryde avatar Jan 28 '15 19:01 ppryde

its the http-upload + data-man package

raix avatar Jan 28 '15 19:01 raix

Looks like that is a no go in terms of fix... interesting nonetheless

I used the following code to populate a helper on page:

unitTestValue: function () {
    var x = new Uint8Array(2);
    x[0] = 7;
    console.log(x[0]);
    console.log(x[1]);
    console.log(x.length);
    return x[0];
}

There does seem to be a difference depending on the way the array is declared but it is not Android/Cordova specific, I encountered the same difference in FF, Chrome and Android App.

new Uint8Array(2) gives 7, 0, 2

whilst

[new Uint8Array(2)] gives 7, undefined, 1

I could only find one line in both packages you mentioned that references Uint8Array as the latter format in data-man (https://github.com/CollectionFS/Meteor-data-man/blob/master/client/data-man-api.js#L279). I forked and ran with the square brackets removed but encountered the same error so reasonably sure this is not the problem, it seems as well supported in the Android web view as it is in the browsers (where it is working).

ppryde avatar Jan 28 '15 21:01 ppryde

@raix Spent some time digging into the code for this and it breaks at this code (https://github.com/CollectionFS/Meteor-cfs-upload-http/blob/master/http-call-client.js#L96-L102):

    var convertToBinary;
    if (options.responseType === "ejson-binary") {
      xhr.responseType = "arraybuffer";
      convertToBinary = true;
    } else {
      xhr.responseType = options.responseType;
    }

It seems to be where the DOM exception is being thrown here and (not sure if symptom or cause) the options.responseType variable is undefined causing it to jump into the else and force the xhr .responseType to be undefined which then throws the DOM error, however only for Android, for browser this is also set to undefined but it doesn't seem to have the same issue.

Forcing options.responseType = "ejson-binary"; fixes my DOM error and will upload the image to the store although it will be useless as this is obviously the wrong type (I get a text file containing '[object Uint8Array]')

Going deeper and back to the initiation of the httpCall in upload-http-client.js and examining the variables in there being passed through for browser vs cordova the only difference I could find was with task.urlParams which only contains the filename parameters is fine in browser but undefined for android uploads (filename:undefined) so this looks like it is possibly further back up the code still but it has been a long day so I am going to pick it up again later (soonish hopefully)

In the meantime any more possible pointers would be awesome!

PS. Your comment RE the objects not being printed is (I think) due to me running it on an android device, the debugger doesn't seem to have any love for console.log with comma separated values but will display the URL and [Object object] respectively for the two when I push them to a separate log i.e. console.log(obj);

ppryde avatar Jan 28 '15 22:01 ppryde

Finally found the culprit to be xhr = new XMLHttpRequest();

There must be something that is phonegap/android specific to this which is screwing it up, not sure what, will research a bit more tomorrow but in browser I can examine the fields within XMLHttpRequest no problem but when I try to pull them out from the android device I get...

Error: "Queue" INVALID_STATE_ERR: DOM Exception 11 [undefined], Error: INVALID_STATE_ERR: DOM Exception 11 [undefined]

which seems very familiar save the number...

ppryde avatar Jan 28 '15 23:01 ppryde

Just to update my misleading info above... and quickly summarize

So the error is indeed caused at this line xhr.responseType = options.responseType; and seems to be when it is passed undefined instead of a blank string.

This is not the issue though as it is something to do with fileobject as far as I can tell, If I force a blank string for this it will upload but the file will have no type and will include only text saying: '[object Uint8Array]. The filename is being passed as undefined although it all looks fine at the point it leaves my app code and sends off the file object as far as I can tell.

I have now tried this on Filesystem and S3 and neither work correctly with android.

Things still for me to try: workaround.... use GridFS debug... take a look at the CFS file package locally

ppryde avatar Jan 30 '15 09:01 ppryde

Tried Grid, FS and S3, unfortunately they all have the same issue. I have copied meteor-cfs-file locally and managed to get it to pick up the correct filename using the name field from the data object here: https://github.com/CollectionFS/Meteor-cfs-file/blob/master/fsFile-common.js#L127-L144

using:

else if (!self.name() && data.name){
        self.name(data.name);
}

this seems necessary as the conditional on typeof data === "string" fails as typeof data is undefined and skips the loop where it attempts to set the name?

But it isnt fixing the main issue that no image is uploaded just the text file with image extension containing [object Uint8Array], somewhere the uInt8Array is breaking or being treated like a string when this file is created.

Any pointers as to a possible area of interest to address this issue would be unbelievably helpful I am working through learning the packages slowly but surely but it is a slow process :D

ppryde avatar Jan 31 '15 00:01 ppryde

Finally got round to solving this issue although there were several different problems masking it, all android/phonegap related problems. Please ignore most of the above as it was mostly me thinking out loud...

Main problem I encountered is highlighted here: https://code.google.com/p/android/issues/detail?id=39882 around. According to the bug devices prior to 4.4 (although the problem also persisted in my newer devices running 4.4.4) basically wont send a blob through XHR, the code just goes meh and CFS throws a DOM EXCEPTION 12 error. The trick is to use a work around for these devices using a buffer instead of a blob which I have now got working perfectly in CFS if I replace the following code in the http-call-client file in Meteor-cfs-upload-http:

https://github.com/CollectionFS/Meteor-CollectionFS/blob/devel/packages/upload-http/http-call-client.js#L97-L102

    if (options.responseType === "ejson-binary") {
      xhr.responseType = "arraybuffer";
      convertToBinary = true;
    } else if (!options.responseType && typeof Uint8Array !== "undefined" && content instanceof Uint8Array){
      //Must set this to text or '' as if we dont an error is thrown when setting reading xhr.response or xhr.responseText
      xhr.responseType = "text";
      //The fix for Android <= 4.4
      content = content.buffer;
    } else {
      xhr.responseType = options.responseType;
    }

The uploads then work(ish) although there are still problems with android < 4.4, none of these are directly related to CFS but I thought they may help someone in a similar situation. I tried a variety of input methods to get the users file on Android and none quite work perfectly but the ones below seemed best and most standard

First: using the will provide everything apart from a MIME type although this can be inferred from either file extension or parsing the file and calculating it, the first I believe is not very robust and the second possibly a tad expensive processing wise

Second: using the camera package to read the file uri (Camera.DestinationType.FILE_URI) from the photo library (Camera.PictureSourceType.PHOTOLIBRARY) which is great but strips off the names of the files for some reason and replaces them with modify.jpg as file name (thanks android), it does however aqcuire the MIME type correctly unlike input as above. This is more workable for me as a file can always be renamed quite easily but still a bit messy as it involve conditional isCordova template to handle file input for browsers etc.

            window.resolveLocalFileSystemURL(imageData, function(fileEntry){
                fileEntry.file(function(filee) {
                        var newFile = new FS.File();
                        newFile.attachData(filee, function (error) {
                          if (error) throw error;
                          Images.insert(newFile, function (error, fileObj) {
                              //DO STUFF
                          });
                        });
                    });
                }, function() {
                    alert('error');
                });
            });

Tried a bunch of other third party file chooser packages and none really did the trick :( although open to suggestions...

ppryde avatar Feb 23 '15 00:02 ppryde

Currently encountering a warning on Google Chrome 43.0.2357.65 for Ubuntu

http-call-client.js:101 The provided value 'undefined' is not a valid enum value of interface XMLHttpRequestResponseType.

in the same code:

if (options.responseType === "ejson-binary") {
  xhr.responseType = "arraybuffer";
  convertToBinary = true;
} else {
  xhr.responseType = options.responseType;
}

It is not a blocking issue but definitively the code may receive undefined in options.responseType which may lead to warnings or errors.

romaroma avatar May 23 '15 05:05 romaroma

@rhyslbw, @raix, I have the same issue as @romaroma. Is it reasonable to make a fast fix like this?

options.responseType = options.responseType || 'text';
if (options.responseType === "ejson-binary") {
  xhr.responseType = "arraybuffer";
  convertToBinary = true;
} else {
  xhr.responseType = options.responseType;
}

michaelrokosh avatar Jun 01 '15 08:06 michaelrokosh

Make sure you are subscribing to your images collection wherever you have your file upload. In my case I saw this error when I forgot to subscribe on my 'new' route. Setting debug mode on startup helped me to determine that the files were in fact getting to the server.

steve-ross avatar Jun 24 '15 20:06 steve-ross

I am getting.. The provided value 'undefined' is not a valid enum value of type XMLHttpRequestResponseType. cfs_upload-http.js:109 in the browser and keeps me from uploading from my app. What can I do for a work around?

projectn00b avatar Dec 03 '15 21:12 projectn00b