0

Essentially what I am trying to achieve here is to check if the Barcode inputted/scanned on the form which is stored in self.trackfile is already in the list of files.

self.files() is an array of arrays, each time the file is added it pushes another array from self.trackfile into self.files(), once all the files have been added into the list they can be 'tracked' and sent back to the server.

I am having trouble getting this to work in IE11 (Compatibility Mode), this works fine in Chrome. I have done some searching around and not found a workaround.

The line var fb = self.files()[x].Barcode(); throws the following error in IE: Object doesn't support property or method 'Barcode'.

If you could help me identify a workaround that would be fantastic!

addFile Script

self.addFile = function () {
    var index = 0;

    if(index < self.files().length){
        var i = 0;

        for (x in self.files()){
            var fb = self.files()[x].Barcode();
            var tb = self.trackfile.Barcode();

            if(fb==tb){
                i += 1;
            }
        }
        if(i > 0){
            alert("Error: File Already Exists in List");
        }
        else {
            self.files.push(new TrackFile(self.trackfile));
        }
   }
   else {
       self.files.push(new TrackFile(self.trackfile));
   }
}

Example of files()

[
  {
    "Location": "Location 1",
    "TransactionMode": "Send",
    "ServicePoint": "Service Point 2",
    "Status": "Incomplete / Open",
    "Comments": "",
    "Barcode": "0123456789",
    "BarcodeImageBase64": ""
  },
  {
     "Location": "Location 1",
     "TransactionMode": "Send",
     "ServicePoint": "ServicePoint 1",
     "Status": "Incomplete / Open",
     "Comments": "",
     "Barcode": "9876543210",
     "BarcodeImageBase64": ""
  }
]

console.log(self.files()[x]); enter image description here

AMouat
  • 685
  • 15
  • 27
  • Could you create a fiddle? – Balázs Feb 09 '17 at 12:46
  • Yeah, i'll give it a go – AMouat Feb 09 '17 at 13:10
  • sorry I am not managing to successfully recreate this in jsfiddle – AMouat Feb 09 '17 at 15:08
  • Have you tried to debug the code and check the value of `self.files()[x]` at the time of the error? – user3297291 Feb 09 '17 at 15:48
  • added a screenshot of the output – AMouat Feb 09 '17 at 16:09
  • The screenshot you've linked shouldn't throw an error. Are you sure it's not the line below that fails? (`self.trackfile.Barcode()`) Also, you might want to declare your `x` variable so that it doesn't get added to the global namespace. Other than that, without an example that reproduces the issue, it's hard to help. – user3297291 Feb 09 '17 at 16:16
  • I have figured out workaround that doesn't require my to return values from nested arrays, I will post my answer in case anyone else hits this problem. Thanks for your suggestions! – AMouat Feb 09 '17 at 16:28

2 Answers2

0

Try looping through your array with indexes instead of the for (x in foo) construct. You're probably running into a random property on the prototype of the array that IE adds which obviously wouldn't contain a Barcode.

See: Why is using "for...in" with array iteration a bad idea?

Community
  • 1
  • 1
Jason Spake
  • 4,293
  • 2
  • 14
  • 22
0

So I figured out how to get around this rather than trying to return a value from a nested array I created an array of just the barcodes:

self.justBarcodes = ko.computed(function() {
    var barcodes = ko.utils.arrayMap(this.files(), function(item) {
        return item.Barcode();
    });
    return barcodes.sort();
}, self);

Then I looped through the self.justBarcodes() array to check if the barcode already exists:

for (var x = 0; x < self.justBarcodes().length; x++){
    var fb = self.justBarcodes()[x];
    var tb = self.trackfile.Barcode();
    if(fb==tb){
        i += 1;
    }
}

Now it works as it should!

AMouat
  • 685
  • 15
  • 27