Smartfield computation function called with zero records as input

Hi forestadmin team! Hope you are all doing well!
I’m totally aware that it is very likely that I introduced that bug in the codebase myself.

Sorry!

Feature(s) impacted

Smart fields in agent nodejs

Observed behavior

When I have a collection with smartfields, and set a filter which matches zero records in the UI, the smart field handler is still called, with zero records.

This causes a crash in the route handler, because I’m doing the following

{
    columnType: 'Number',
    dependencies: ['id'],
    getValues: async (records, context) => {
       //                         ^---- this is an empty array!
       const rows = await context.collection.nativeDriver.rawQuery(`
         SELECT blabla FROM whatever WHERE modelId IN (:recordIds)`,
         { recordIds: records.map(r => r.id) }
      );

     return records.map(...)
}

Expected behavior

As there is nothing to compute, the handler should not be called.

The workaround on customer side is trivial but I need to repeat it on all smart fields.
I would love to avoid that :slight_smile:

if (!records.length) return []

Failure Logs

image
=>
image

Context

  • Project name: roundtable
  • Team name: …
  • Environment name: prod
  • Agent (forest package) name & version: agent nodejs 1.36.10
  • Database type: sql
  • Recent changes made on your end if any:
1 Like

Hey @Romain_Gilliotte :wave:

I hope you’re doing great. :slight_smile:

You are right, we should do something about it to avoid the anti-pattern on the client side.
I’m creating a tiny PR to fix this tiny issue.

I will get back to you once released.

Cheers,
Morgan

And it’s done. It’s live with the latest version of the Node.js agent → @forestadmin/agent@1.38.5. :partying_face:

We hope that it will help you with your use case. :muscle:

Thanks for your report, it helps us to still get better. :pray:

Kind regards,
Morgan

That was fast!
Thanks morgan

1 Like