eval 或替代方法通过函数内 then() 内的变量调用对象

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

我在一个函数中导​​入了'Bank',我想在then()里面使用。

我正在使用 eval(table) 但出现错误: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()
     }
   }
 }

调用此函数:

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

你为什么要使用 eval

你应该 switch to accessing properties by name:

import {  Bank } from './ormconnectors';
const tableByName = {"Bank": Bank};

...

        return tableByName[table].findAll()