-1

I am trying to perform validation on a json object. But it seems that the method ispoQuantityInvalid method is not getting called at all.

Fiddle

selectedRows = [{
    "itemNumber": "",
    "lineNumber": 1,
    "description": "PUMP,COMPLETE, P/N E12LYTFS-137",
    "uom": "",
    "poQuantity": "1",
    "recievedQuantity": "3",
    "isMatched": false,
    "p2PLineItemId": "168512",
    "quantityUnitPrice": "1",
    "buyerItemNumber": null
}];    


 ispoQuantityInvalid = () => (element, index, array) => {
 alert() //not getting called. 
    return this.isEmpty(element.poQuantity);
  }
console.log(this.selectedRows)

if (this.selectedRows.some(this.ispoQuantityInvalid)) {
     console.log('po qty is null or empty') //always gets called.
      return;
    }    

  isEmpty = (value) => {     
    return (value == null || value.length === 0 || value === '' || value == 0);
  }
  1. Even though poQuantity is not null or empty or 0, it is failing validation.
  2. It seems ispoQuantityInvalid method is not getting called at all. I don't see alert.

What am I missing here? It seems to be a scope issue. But I am using arrow function that is supposed to not cause this issue right?

Maulik Sakhida
  • 471
  • 4
  • 15
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • `() => (element, index, array) => {// do work}` is not valid syntax. If want an arrow function without passing anything you'd use `() => {// do work}` if you want to pass one parameter, use `foo => {// do work}`. If you need to pass more than one, use `(foo, bar) => {// do work}` – volt Jan 19 '20 at 18:19
  • @volt thanks , it is calling the function but validation is still failing `this.isEmpty is not a function` – SamuraiJack Jan 19 '20 at 18:25
  • `ispoQuantityInvalid = …` is creating a global variable, but `this.ispoQuantityInvalid` might be referencing something else. Make sure to get the scope of your variables right. – Bergi Jan 19 '20 at 18:29
  • 1
    @volt It [is valid](https://stackoverflow.com/questions/32782922/what-do-multiple-arrow-functions-mean-in-javascript), but you're right that it doesn't do what the OP wants – Bergi Jan 19 '20 at 18:30

1 Answers1

1

change

 ispoQuantityInvalid = () => (element, index, array) => {
 alert() //not getting called. 
    return this.isEmpty(element.poQuantity);
  }

to

 ispoQuantityInvalid = (element, index, arra) => {
 alert() //not getting called. 
    return this.isEmpty(element.poQuantity);
  }

The first one is a function that returns the function you want to call. Therefore the inner function is only available but never called. The if-statement then looks like if( function(e,i,a) {} ) which is not false, so the if statement body gets executed.

marks
  • 1,501
  • 9
  • 24
  • the function gets called now but but It is still failing. `this.isEmpty is not a function` – SamuraiJack Jan 19 '20 at 18:25
  • simply remove the "this" before isEmpty. Or, if you're in object context and need the this (which seems to be the case after i looked at you code again), bind whatever "this" is into the function you are calling: if (this.selectedRows.some(this.ispoQuantityInvalid.bind( this ))) – marks Jan 19 '20 at 18:28