0

I've got probelm with filtering array from form values.

I using angular 7 and express js

export function getItems(req: Request, res: Response) {
  const itemCriteria: ItemCriteria = req.body.itemCriteria;
  const name = (itemCriteria.name) ? itemCriteria.name : '';
  const description= (itemCriteria.description) ? 
itemCriteria.description: '';
  const category= itemCriteria.category;

  const filterItems: Item[] = items
    .filter(item=> item.category.id === category &&
               item.name .includes(name ) &&
               item.description.includes(description)
    );

  res.status(200).json(filterItems);
}

When I fill all fields filterItems is correct, but when I fill only one or two fields I've got error 'Cannot read property of undefinied'. This fields is from reactive form but only one is required else is optional. So my question is how can I filter this array correct?

5
  • What is the value of items, firstName and lastName? Commented Jun 23, 2019 at 18:18
  • Sorry my bad, I wrote it wrong, but problem is still exist Commented Jun 23, 2019 at 18:31
  • 1
    Will you share your frontend code as well please? Commented Jun 23, 2019 at 18:32
  • where do you get the error? frontend or backend? Commented Jun 23, 2019 at 18:37
  • Error is in backend my fake node js server. This values: name, description, category is from frontend (reactive forms values) Commented Jun 23, 2019 at 18:47

2 Answers 2

1

When one of Criteria is empty, you don't have to include it in the filter function.

So you can change it by following.

const filterItems: Item[] = items
  .filter(item=> (!category || (item.category && item.category.id === category)) &&
    (!name || (item.name && item.name.includes(name))) &&
    (!description || (item.description && item.description.includes(description))
  );
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to read a property of an undefined variable. It's hard to know which one it is, but you can protect your code from these types of errors by making sure a variable is defined like this:

const myProperty = someVariable && someVariable.someProperty

This shortcircuits if someVariable is falsy.

In your case this might look like:

const filterItems: Item[] = items
    .filter(item=> item && item.category && item.category.id === category &&
               item.name && item.name.includes(firstName) &&
               item.description && item.description.includes(lastName)
    );

(also it's not clear where firstName, lastName, and items come from, but I don't think that's the cause of the error you're asking about)

1 Comment

Yeah, my bad when I wrote post here whith variables. Your solution is good because I don't have error but filters array is no correct :/ I think the problem is I filter values but few might be empty for example: Form: category is 1, name is 'Item1' description is empty. So statement whith description is not met

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.