TypeError: Cannot read property 'fields' of undefined after migrating from v5 to v8 on forest-express ResourceSerializer when getting fields names

Feature(s) impacted

Relationships in related datas are not working due to an error in forest-express RessourceSerializer

Observed behavior

When i click on the related data menu the query is executed on my api and the exception is thrown when i call the serializer :

const serialized = await new Serializer(Liana, Letters, results.docs, null, {
  count: results.total,
}).perform()

The exception source appears to be on line 157 when getting fields names :

image

Expected behavior

No error

Failure Logs

7|API | 2022-02-16T15:18:44.733Z app:utils:errors TypeError: Cannot read property ā€˜fieldsā€™ of undefined
7|API | at ResourceSerializer.perform (**************************************\node_modules\forest-express\dist\serializers\resource.js:157:44)
7|API | at **************************\forest.js:1161:16

Context

Please provide in this mandatory section, the relevant information about your configuration:

  • Project name: Seeuletter - API
  • Team name: Admin
  • Environment name: Development or Staging
  • Agent type & version: 8.5.0
  • Recent changes made on your end if any: v5 ā†’ v8

Hi @Olivier_BESSON1,

Did you override that route ? If yes could you please share your code :pray:

@Olivier_BESSON1,

Did you follow the migration documentation to upgrade?

Thanks.

Hi @vince
Iā€™m not sure of what you mean by ā€œoverride that routeā€ ?

The source that receive the query is this :

router.get('/forest/users/:userId/relationships/letters_test', Liana.ensureAuthenticated, ForestApi.display_test_letters_in_user)

And in ForestApi.display_test_letters_in_user the source that crash is

const serialized = await new Serializer(Liana, Letters, results.docs, null, {
  count: results.total,
}).perform()

Hi @anon34731316 my developpement studio has followed the documentation because i gave them the links and told them to follow this.

Could you then please share the code of the function ForestApi.display_test_letters_in_user

export async function display_test_letters_in_user(req: Request, res: Response): Promise<void> {
  debug('req.body : %O', req.body)
  debug('req.params : %O', req.params)
  debug('req.query : %O', req.query)

  try {
    const results = await Letters.paginate(
      {
        user: req.params.userId,
        mode: 'test',
      },
      {
        page: parseInt((req.query.page as any).number, 10),
        limit: parseInt((req.query.page as any).size, 10),
        sort: req.query.sort,
        populate: [
          {
            path: 'file',
            select: '-__v',
          },
          {
            path: 'delivery_proof',
            select: '-__v',
          },
          {
            path: 'original_file',
            select: '-__v',
          },
          {
            path: 'filing_proof',
            select: '-__v',
          },
          {
            path: 'events',
          },
        ],
      }
    )

    debug('results.total : ', results.total)

    const serialized = await new Serializer(Liana, Letters, results.docs, null, {
      count: results.total,
    }).perform()

    res.send(serialized)
    return
  } catch (ex: any) {
    sendError(ex)
    res.status(400).json({
      error: ex.message,
    })
    return
  }
}

Iā€™m a bit curious. The serialization seems a bit outdated. COuld you please try to replace:

    const serialized = await new Serializer(Liana, Letters, results.docs, null, {
      count: results.total,
    }).perform()

by

const { RecordSerializer } = require('forest-express-mongoose');

const serializer = new RecordSerializer(Letters);
const serialized = serializer.serialize(results.docs, { count: results.total }));

@vince I tried this, before your return, but serialize prototype accept only one argument :

So i tried, before before your return, this code :

    const serializer = new RecordSerializer(Letters)
    const serialized = await serializer.serialize(results.docs)

The result was returned but on browser i saw on pagination ā€œ1 OF NANā€ so navigation would be impossible

When you asked me to tried, it make me think to try this :

    const serializer: any = new RecordSerializer(Letters)
    const serialized = await serializer.serialize(results.docs, { count: results.total })

And it seems to work.

Is there a bug on serialize prototype declaration ?
Is my last code good and must work ?

1 Like

@vince @anon34731316 Can you tell me if my solution seems good and why the RecordSerializer.serialize has only 1 parameter ?

Hey @Olivier_BESSON1 sorry for the delay,
Yes itā€™s a type declaration mistake. Iā€™ll fix it