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.
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
After making these changes, I triggered an error when update users collection, TransactionManger works as expected
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.