Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

406
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!