0

I have this error on my console

below is the snippet of the code i am working with

JAVASCRIPT

get_items: function (item_code)

if (key) {
        if (key.length == 13){
            if (key.startsWith('221')){
                    return $.grep(this.item_data, function (item) {
                    if(item.barcode !== null && item.barcode !== '') {

                           if (item.barcode.substr(0,7) == key.substr(0,7)) {                             
                                    pesokg1 = key.substr(7,6)

                                    if (pesokg1.startsWith('0000')){
                                            pesokg='0.' + pesokg1.substr(4)
                                    }else if (pesokg1.startsWith('000')){
                                            pesokg='0.' + pesokg1.substr(3)
                                    }else if (pesokg1.startsWith('00')){
                                            pesokg='0.' + pesokg1.substr(2)
                                    }else if (pesokg1.startsWith('0')){
                                            pesokg=pesokg1.substr(1,1) +'.' + pesokg1.substr(2,pesokg1.length)
                                    }else if (!pesokg1.startsWith('0')){
                                            pesokg=pesokg1.substr(0,2) +'.' + pesokg1.substr(2,pesokg1.length)

                                    }

                                    search_status = false;
                                    return true
                            }
                    }
                    })

            }
      }
EnSeal
  • 11

1 Answers1

1

Seems like the line

if(item.barcode !== null && item.barcode !== '') {

is missing the case where the item does not have a barcode. So either add in undefined check

if(item.barcode !== null && item.barcode !== undefined && item.barcode !== '') {
  /* the code */
}

or just use a truthy check

if(item.barcode) {
  /* the code */
}
epascarello
  • 204,599
  • 20
  • 195
  • 236