feathers-mongoose: Mongo-DB Transaction - ReferenceError: when is not defined

350 Views Asked by At

I am using Feathers 4.5.0 and Mongo 5.0.2 . I want to perform Mongo DB transactions on a collection. I copied the sample in feather-mongoose documentation into my service hook.

const TransactionManager = require('feathers-mongoose').TransactionManager;
const isTransactionEnable = process.env.TRANSACTION_ENABLE || false;
const skipPath = ['login'];

let moduleExports = {
  before: {
  all: [],
  find: [],
  get: [],
  create: [
    when(isTransactionEnable, async hook =>
     TransactionManager.beginTransaction(hook, skipPath)
   )
  ],
  update: [
   when(isTransactionEnable, async hook =>
     TransactionManager.beginTransaction(hook, skipPath)
   )
  ],
  patch: [],
  remove: []
},

after: {
  all: [],
  find: [],
  get: [],
  create: [when(isTransactionEnable, TransactionManager.commitTransaction)],
  update: [when(isTransactionEnable, TransactionManager.commitTransaction)],
  patch: [],
  remove: []
},

error: {
  all: [],
  find: [],
  get: [],
  create: [when(isTransactionEnable, TransactionManager.rollbackTransaction)],
  update: [when(isTransactionEnable, TransactionManager.rollbackTransaction)],
  patch: [],
  remove: []
 }
};

module.exports = moduleExports;

And I get this error message ReferenceError: when is not defined. I remove the "when" and the transaction still did not work.

1

There are 1 best solutions below

0
On

If you have issues setting up MongoDB Replicas locally, this youtube tutorial was useful for me when I was navigating this issue. Your replica DB must be working before feather-mongoose TransactionManger can do its job. I made some adjustments to the example code

const TransactionManager = require('feathers-mongoose').TransactionManager;
const isTransactionEnable = process.env.TRANSACTION_ENABLE || false;
const skipPath = ['login'];


module.exports = {
  before: {
  all: [],
  find: [],
  get: [],
  create: [(isTransactionEnable, async hook =>
    TransactionManager.beginTransaction(hook, skipPath)
  )],
  update: [(isTransactionEnable, async hook =>
    TransactionManager.beginTransaction(hook, skipPath)
  )],
  patch: [(isTransactionEnable, async hook =>
    TransactionManager.beginTransaction(hook, skipPath)
  )],
  remove: []
},

after: {
  all: [],
  find: [],
  get: [],
  create: [(isTransactionEnable, async () => {await TransactionManager.commitTransaction;})],
  update: [(isTransactionEnable, async () => {await TransactionManager.commitTransaction;})],
  patch: [(isTransactionEnable, async () => {await TransactionManager.commitTransaction;})],
  remove: []
},

error: {
  all: [],
  find: [],
  get: [],
  create: [(isTransactionEnable, async () => {await TransactionManager.rollbackTransaction;})],
  update: [(isTransactionEnable, async () => {await TransactionManager.rollbackTransaction;})],
  patch: [(isTransactionEnable, async () =>{await TransactionManager.rollbackTransaction;})],
  remove: []
}
};

After making these changes, I triggered an error when update users collection, TransactionManger works as expected

 const newUser = app.service('users').create({name:'ope', gender:'Male')}
 app.service('users').patch(newUser._idl,{gender: 'Female'}); //this fails and transaction rollback users collection to initate state. 

This line of code will fail because it is expecting newUser._id but newUser._idl was provided. Hence, user "ope" will be remove from DB. I hope this helps someone.