Always getting not null object even though there are no objects with these parameters;

Hi @komi94, I think you’re very close, the problem is a semantics one.

Let’s break down what I think is happening:

First, you’re doing a peekAll, this returns a RecordArray, which, even if there are 0 records will still be a non-null object of type RecordArray. No problem there.

Next, you’re filtering (I’m assuming that’s what the grades.find is doing at least), all good there. However note that if you filter all the records out you’re left with an empty RecordArray, that’s important in the next part.

Last you get to the part where you’re saying grades.get('firstObject') !== null which I think is the problem. What you’re doing here is getting the first object from a (in this case at least) empty RecordArray, and then strictly comparing to not null. However if you try to get the firstObject from a RecordArray, it will not return null, it will return undefined, and in javascript null and undefined are different values when using strict comparison. So essentially, undefined !== null, which evaluates to true.

To fix this you have several options. My personal choice would be to say:

if(grades.length) {
  // this will be evaluated if the the length of the array is anything other than 0, e.g. if there are any records in grades

because what you’re really doing is looking at length of the grades RecordArray. However if you still wanted to look at the first object you could either say:

if(grades.get('firstObject')) {
  // this will be evaluated if the firstObject is ANY truthy value, e.g. not undefined, not null, not false, etc.


if(grades.get('firstObject') !== undefined) {
  // this will be evaluated if the firstObject is strictly not undefined, e.g. if the record array is empty