0

I have an HTML element as bellow

<div *ngFor="let item of items"> <input type='number' [min]="getMinData(param1,param2)" /> </div> 

inside ts file

 getMinData(rules: ConstraintRule[], id: string) { rules.forEach((rule) => { rule._metadata.productList.forEach((product: SProduct) => { product.OptionGroups.forEach((optionGroup: SProductOptionGroup) => { optionGroup.Options.forEach((option: SProductOptionComponent) => { if (option.ComponentProductId == id) { return option.MinQuantity; } }); }); }); }); } 

Also, there's no any API call or any other thing, it's just filtration over the local data.

But, On HTML correct qty is not returned.

9
  • Array.forEach is not asynchronous, it is blocking. Your code has to wait for it to finish, that is not your issue. Commented Jun 16, 2021 at 14:11
  • @Alex Well if I do console on the last statement, I get the correct value, but on the HTML side even if I print this I don't get any value. Commented Jun 16, 2021 at 14:12
  • @Alex Do you see any other issue? Commented Jun 16, 2021 at 14:13
  • You're not returning anything Commented Jun 16, 2021 at 14:18
  • 1
    That's just returning from the forEach loop, not the function. The function getMinData always returns undefined. Commented Jun 16, 2021 at 21:00

1 Answer 1

1

You have to return something for getMinData function.

Currently, you call return option.MinQuantity;, but it is result for an anonymous function - (option: SProductOptionComponent) => {.

If you just want to find the first option.MinQuantity, try this function:

getMinData(rules: ConstraintRule[], id: string) { let minQuantity = 0; rules.forEach((rule) => { rule._metadata.productList.forEach(({ OptionGroups }) => { OptionGroups.forEach(({ Options }) => { const foundOption = Options.find((o) => o.ComponentProductId === id); minQuantity = foundOption ? foundOption.MinQuantity : 0; // Default min value is 0 }); }); }); return minQuantity; // return value } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.