Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

405
Vistas
Converting string to a type (node.js)

I know this seems like an odd problem, but at the moment I'm creating a webserver to save data to a mongodb database using mongoose. To use that, I need to create a model with a schema which basically defines the values I can search to, as well as the type they should be (String, Number, etc.)

As I said before, since it's a webserver, I'd like to make it so the webserver will create the model and the schema, with a body object in the request. However, there is an issue. I can only pass the type (String, Number, etc.) as a string. I've come up with a solution to parse the value into a type, that being the code below

    getTypeByString: function (string) {
        if (string == 'String') {
            return String
        }
        if (string == 'Number') {
            return Number
        }
        if (string == 'Array') {
            return Array
        }
        if (string == 'Object') {
            return Object
        }
    }

However, I feel like there is probably a more simple solution. If there is or isn't, please let me know! I'd like to release my code on GitHub eventually, and I'd like it to be as dynamic and simple as possible. Thank you in advance

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use a switch statement to make the code a bit simpler:

getTypeByString: function(stype){
    switch(stype){
        case "String": return String;
        case "Number": return Number;
        case "Array": return Array;
        case "Object" : return Object;
        default: return null; // or throw an error
    };
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

String, Number, Array and Object are also properties of the global variable (or window variable if you are in a browser).

You can check it yourself by evaluating:

global['String'] === String // true

For this reason, you can just use the string to lookup the type within the global object, and return it:

getTypeByString: function (string) {
  if (['String', 'Number', 'Array', 'Object'].includes(string)) // be sure that you are actually returning a type
    return global[string];
  return null;
about 4 years ago · Juan Pablo Isaza Denunciar

0

In regards to getting a defined type from a string you can use an object to define the types and use the string as the key.

const DefinedTypes = {
  String: String,
  Number: Number,
  Array: Array,
  Object: Object,
  CustomType: classOfCustomType
};

Using it.

const typeAsString = 'String';
const type = DefinedTypes[typeAsString];
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda