Pass a string as a parameter in JS

This is pretty simple, I’m trying to insert a value that has a string as a parameter. Let’s give an example: const stringName = "User". and my goal is to activate a function that gonna be called like that User.find(); I tried the follows without success: stringName.find() but didn’t worked and also [stringName].find()

Why I want to do that: I receive a param that is a string, and I want to search with this param in my DB so the real query gonna be something like that: param.findAll({where....})

I’m sure that there is a simple solution like the examples above. hope for help.

Answer

You can store User in an object, and then access it with the string.

const myObjects = {
  User: User,
  // Post: Post, ...
}
const stringName = "User"
myObjects[stringName].find('foo')