Search code examples
javascriptecmascript-6eval

eval or alternative to call an object through a variable inside then() inside a function


I import 'Bank' in a function, and i want to use inside in then().

I'm using eval(table) but i get th error: ReferenceError: Bank is not defined',

import {  Bank } from './ormconnectors';

const genericResolver = ( table, action , values ) => {

  if (action==='list') {

    const errors = [];
    return Auth.isAuthenticated()
      .then(() => {
        return eval(table).findAll()
     }
   }
 }

calling this function:

genericResolver  ( 'Bank', ..... );

Solution

  • Why are you using eval at all?

    You should instead switch to accessing properties by name:

    import {  Bank } from './ormconnectors';
    const tableByName = {"Bank": Bank};
    
    ...
    
            return tableByName[table].findAll()