简体   繁体   English

将字符串转换为类型(node.js)

[英]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.我知道这似乎是一个奇怪的问题,但目前我正在创建一个网络服务器来使用 mongoose 将数据保存到 mongodb 数据库。 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.)要使用它,我需要创建一个 model ,其架构基本上定义了我可以搜索到的值以及它们应该是的类型(字符串、数字等)

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.正如我之前所说,因为它是一个网络服务器,所以我想让它这样网络服务器将创建 model 和模式,并在请求中包含一个主体 object。 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.如果有或没有,请告诉我,我想最终在 GitHub 上发布我的代码。 and I'd like it to be as dynamic and simple as possible.我希望它尽可能动态和简单。 Thank you in advance先感谢您

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.关于从字符串中获取定义的类型,您可以使用 object 来定义类型并使用字符串作为键。

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

Using it.使用它。

const typeAsString = 'String';
const type = DefinedTypes[typeAsString];

You could use a switch statement to make the code a bit simpler:您可以使用 switch 语句使代码更简单一些:

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
    };
};

String , Number , Array and Object are also properties of the global variable (or window variable if you are in a browser). StringNumberArrayObject也是global变量的属性(如果您在浏览器中, window变量)。

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:因此,您可以只使用字符串在global object 中查找类型,然后返回:

getTypeByString: function (string) {
  if (['String', 'Number', 'Array', 'Object'].includes(string)) // be sure that you are actually returning a type
    return global[string];
  return null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM