Unhandled rejection TypeError: Cannot read property 'renderingId' of undefined after upgrading to forest-express-sequelize V8

Expected behavior

While running on Version 7, I used the following function to update smart fields.

function updateTranslation(params) {
  let { label, value, field } = params;
  console.log("params: ", params);

  return new ResourceGetter(Label, { recordId: label.id })
    .perform()
    .then((foundLabel) => {
      return Translation.findOne({
        where: { translationKey: foundLabel.key },
      });
    })
    .then((foundTranslation) => {
      foundTranslation[field] = value;
      foundTranslation.save();
      return foundTranslation;
    });
}

Actual behavior

The code is throwing an error when executing .perform()

Failure Logs

Unhandled rejection TypeError: Cannot read property 'renderingId' of undefined
    at ScopeManager._callee2$ (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/services/scope-manager.js:92:26)
    at tryCatch (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/regenerator-runtime/runtime.js:294:22)
    at Generator.next (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/regenerator-runtime/runtime.js:119:21)
    at asyncGeneratorStep (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /Users/camillefeghali/dev/foxxbee/forest-api/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at ScopeManager.<anonymous> (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)
    at ScopeManager.getScopeForUser (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/services/scope-manager.js:125:33)
    at ResourceGetter.perform (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express-sequelize/dist/services/resource-getter.js:26:60)
    at updateTranslation (/Users/camillefeghali/dev/foxxbee/forest-api/forest/label.js:31:6)
    at Object.set (/Users/camillefeghali/dev/foxxbee/forest-api/forest/label.js:10:7)
    at /Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/deserializers/resource.js:47:22
From previous event:
    at processImmediate (internal/timers.js:439:21)
From previous event:
    at extractAttributes (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/deserializers/resource.js:44:14)
    at ResourceDeserializer.perform (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/deserializers/resource.js:96:12)
    at Resources.update (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/routes/resources.js:98:81)
    at Layer.handle [as handle_request] (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/express/lib/router/route.js:137:13)
    at _callee$ (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/forest-express/dist/middlewares/permissions.js:117:19)
    at tryCatch (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/regenerator-runtime/runtime.js:294:22)
    at Generator.next (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/regenerator-runtime/runtime.js:119:21)
    at asyncGeneratorStep (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/camillefeghali/dev/foxxbee/forest-api/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

Context

Could this be cause by the upgrade to version 8 ?

  • Package Version: ^8.0.5
  • Express Version: ^4.17.1
  • Sequelize Version: ^6.6.2
  • Database Dialect: postgres
  • Database Version: 11
  • Project Name: forest-api

Hello @cooki !

It is indeed due to the upgrade to the V8.

The migration guide is a little bit hidden in the documentation, sorry for that!
Here is the link: https://docs.forestadmin.com/documentation/how-tos/maintain/upgrade-notes-sql-mongodb/upgrade-to-v8#scopes

The prototype of the ResourceGetter constructor has changed.
If should now be: new RecordsGetter(MyModel, req.user, req.query).

Good luck with your migration!

1 Like

Hey @anon39940173 , thanks for getting back to me, there is no mention of the request object when trying to update a smart field. Create and manage Smart Fields - Documentation

Could you tell me how a set callback function would look like ? currently the docs show the following:

const { collection } = require('forest-express-sequelize');

collection('customers', {
  fields: [{
    field: 'fullname',
    type: 'String',
    get: (customer) => {
      return customer.firstname + ' ' + customer.lastname;
    },
    set: (customer, fullname) => {
      let names = fullname.split(' ');
      customer.firstname = names[0];
      customer.lastname = names[1];

      // Don't forget to return the customer.
      return customer;
    }
  }]
});

Thanks

Using sequelize directly should do the job.

async function updateTranslation(params) {
  let { label, value, field } = params;

  const labelWithKey = await Label.findOne({
    where: { id: label.id },
    attributes: ['key']
  });

  await Translation
    .update({[field]: value})
    .where({ translationKey: labelWithKey.key })
    .limit(1)
}

@anon39940173, yup, that’s what i ended up doing, Thanks !