I am currently developing an accounting system that allows users to dynamically create accounts in the chart of accounts. Each time a new account is created, the app also generates a new collection for that specific account. Here is an example:
const account = new Chart({
name: accountName,
category: accountCategory
});
After creating the account and adding it to the charts collection, I need to create a separate collection for that specific account. I attempted to call a new method to accomplish this, which looks like:
const createModel = (accountName)=>{
const newModel = mongoose.model(accountName, ledgerSchema);
}
This successfully creates a model, and I can immediately add a document to it as shown below:
const createModel = (accountName)=>{
const newModel = mongoose.model(accountName, ledgerSchema);
const record = new accountName({
some_value: 100
});
record.save();
}
The issue arises when I try to create documents and add them to the model at a later point or in another function, as it only seems to work during the model declaration phase. If I attempt to do the following:
const record = new accountName({
some_value: 100
});
record.save();
}
In any other function or after the application has been closed, it fails with the error message stating that accountName is not defined. Assistance would be greatly appreciated.