# Check a field of a non-existent item in the database \*\*

**URL:** <https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512>\
**Category:** Ask us anything\
**Created:** [March 11, 2024, 6:25pm UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512 "2024-03-11T18:25:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lianr7](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@lianr7](https://community.weweb.io/u/lianr7)\
**Post date:** [March 11, 2024, 6:25pm UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512/1 "2024-03-11T18:25:03Z")

</div>

Hi, guys!

I have a problem that I’ve had in other situations and I don’t know how to solve it.  
To understand the context, my application has courses, each course can have N modules and classes that belong to 1 module and 1 course. Each module has 1 index number to be able to sort.

When registering a module, I am using an “if” operator to see if there is already a module registered in that course. If there is already a module registered, I search for the value of the highest index in the list of modules (let’s assume that there are 3 modules in the English course with indexes 1, 2 and 3, then the new module will be registered with index 4). If there is no module in that course, it will register a course with index 1.

The problem is this: even though there is no course registered, the formula does not work because “Cannot read properties of undefined (reading ‘index’)”.

How to get around this?

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/f/f23ee90c0adc9c59c5ab3d007d39b927362435a6.png)

---

<div class="post-metadata">

**Author:** ![Dominic](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dominic/32/5645_2.png) [@Dominic](https://community.weweb.io/u/Dominic)\
**Post date:** [March 11, 2024, 9:33pm UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512/2 "2024-03-11T21:33:52Z")

</div>

@lianr7 lodash \_maxBy returns maximum value as integer not an array. Try the same fomula without .index at the end.

---

<div class="post-metadata">

**Author:** ![lianr7](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@lianr7](https://community.weweb.io/u/lianr7)\
**Post date:** [March 11, 2024, 10:21pm UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512/3 "2024-03-11T22:21:31Z")

</div>

It returns the object, apparently

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/2/2971b5c4128559e2bbffe4816453aeedadcd32b0.png)

---

<div class="post-metadata">

**Author:** ![Dominic](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dominic/32/5645_2.png) [@Dominic](https://community.weweb.io/u/Dominic)\
**Post date:** [March 11, 2024, 11:32pm UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512/4 "2024-03-11T23:32:26Z")

</div>

Hmmm… that’s weird - maybe try to use getKeyValue on that object. It apparently doesnt recognize dot notation on this NPM module.

---

<div class="post-metadata">

**Author:** ![lianr7](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@lianr7](https://community.weweb.io/u/lianr7)\
**Post date:** [March 11, 2024, 11:48pm UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512/5 "2024-03-11T23:48:18Z")

</div>

Ah, I just had an idea! What do you think if I create an action first, like “return a value”, that takes an array that contains the index list of each item and then compares it with the largest value in the returned array

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [March 12, 2024, 7:33am UTC](https://community.weweb.io/t/check-a-field-of-a-non-existent-item-in-the-database/7512/6 "2024-03-12T07:33:18Z")

</div>

all the parameters of an if formula are evaluated because it’s a function. You need to use an if else statement ina workflow or a [ternary operaror](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator) in a formula to actually evaluate only one of the two options

> [@All instructions in a condition are executed](https://community.weweb.io/t/all-instructions-in-a-condition-are-executed/797/4):
>
> wwFormulas.if is a function not a if else statement. Assignments or other functions in the parameters are all executed. You should use an if else statement.
