【发布时间】:2020-01-19 18:08:14
【问题描述】:
我正在尝试对 json 对象执行验证。但似乎方法 ispoQuantityInvalid 方法根本没有被调用。
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);
}
- 即使
poQuantity不为空、不为空或不为0,它也未通过验证。 - 似乎
ispoQuantityInvalid方法根本没有被调用。我没有看到警报。
我在这里缺少什么? 这似乎是一个范围问题。但是我使用的箭头功能应该不会导致这个问题,对吧?
【问题讨论】:
-
() => (element, index, array) => {// do work}语法无效。如果想要一个箭头函数而不传递任何东西,如果你想传递一个参数,你会使用() => {// do work},使用foo => {// do work}。如果需要传递多个,请使用(foo, bar) => {// do work} -
@volt 谢谢,它正在调用该函数,但验证仍然失败
this.isEmpty is not a function -
ispoQuantityInvalid = …正在创建一个全局变量,但this.ispoQuantityInvalid可能正在引用其他内容。确保正确设置变量的范围。 -
@volt It is valid,但你是对的,它没有做 OP 想要的
标签: javascript typescript validation ecmascript-6 scope