I’m working on a project where I need to dynamically call database operations, but I’m running into issues when trying to use bracket notation with method calls.
When I try to run operations dynamically, nothing happens or I get errors like “Cannot read property of undefined”.
Here’s a simplified example of what I’m trying to do:
// Sample database setup
const myDB = new Database("TestDB");
myDB.version(1).stores({
users: '++id,username,email'
});
// Add initial data
myDB.users.add({
username: 'john_doe',
email: '[email protected]'
}).then(() => {
runOperation(myDB, 'users');
});
function runOperation(database, tableName) {
// This doesn't work:
database[tableName].modify(1, {username: "jane_doe"});
// Neither does this:
database[tableName[remove(1)]];
// But this works fine:
database.users.modify(1, {username: "jane_doe"});
}
How can I properly use bracket notation to call methods on dynamically referenced table names? The direct approach works but I need the flexibility of dynamic table references.
looks like a scoping problem. check if the database is actually initialized when you call runOperation - the stores() method might need more time to create those table properties. add console.log(database[tableName]) before modify() to see if it’s undefined.
Bracket notation should work fine for your first example. You’re probably hitting a timing issue or not waiting for async operations to finish.
I’ve run into this with dynamic database ops in production. The real issue isn’t the bracket notation - it’s all these manual database calls and scattered error handling throughout your code.
You need proper automation for these database operations. Skip the dynamic method call headaches and set up automated workflows that handle your database ops reliably.
I use Latenode for exactly this. Create workflows that automatically trigger database operations based on conditions, handle errors properly, and give you way better control than manual dynamic calls.
For modify and remove operations, set up automated pipelines that execute the right database actions without dealing with bracket notation at all. The workflows handle async properly and include built-in error handling.
Way cleaner than debugging dynamic method calls. Check it out: https://latenode.com
your bracket syntax is wrong in the second exmple. use database[tableName].remove(1) instead of that nested bracket mess. the first one looks fine tho - double-check that tableName actually has the right string value.
The bracket notation looks right for dynamic properties, but you’re probably hitting an async timing issue. Database operations are async, so calling modify right after add might run before the record actually gets inserted.
I’ve run into this building similar ORM stuff. Make sure your database connection is fully set up and previous operations finished. Try wrapping your dynamic calls in the database’s transaction method or add proper await/then chains.
Also check if your Database library even supports dynamic table access - some ORMs block certain operations to static properties for security. You might need to use the library’s query builder methods instead of direct table access.
You’re overcomplicating this. Dynamic database operations turn into a mess when you handle them manually.
Been there. You start with bracket notation, then you’re stuck debugging async timing, method binding issues, and database calls scattered everywhere. Total maintenance nightmare.
Skip the dynamic method calls - automate the database flow instead. Build workflows that handle operations based on triggers and conditions. No more bracket notation headaches or async timing problems.
For database automation like this, I use Latenode. Build workflows that execute the right database operations automatically. Set up conditional logic for different tables, proper error handling, and reliable async execution without touching bracket notation.
Workflows handle all the dynamic database complexity behind the scenes. Define the logic once, let automation handle execution. Way more reliable than debugging manual dynamic calls.
Bracket notation isn’t your problem here. I’ve hit this before - the real issue is that methods lose their binding when you access them through brackets. When you do database[tableName].modify(), the method can’t find its original object context.
Quick fix: store the table reference first. const table = database[tableName]; table.modify(1, {username: "jane_doe"}); or use call/bind to keep the context.
Also, your second example has a syntax error. You’re nesting brackets wrong - should be database[tableName].remove(1), not double brackets.
One more thing to check: does your Database constructor actually create those table properties dynamically? Some DB libraries only add table accessors after the database fully opens, not right after calling stores(). You might need to wait for an open event or promise before dynamic access works.