简体   繁体   English

JSON(节点/表达式)和JSON数组中的自定义“类型”

[英]Custom “types” in JSON (node/express) and JSON arrays

I recently had to convert a project that had a lot of work done with ASP.NET Core 1.0 to Node.js. 最近,我不得不将一个使用ASP.NET Core 1.0完成许多工作的项目转换为Node.js。 I'm absolutely out of my comfort zone, knowing hardly anything about JavaScript and having had the good life of ASP serializing C# classes for me without me having to do the JSON myself. 我绝对不在我的舒适范围内,对JavaScript几乎一无所知,并且拥有ASP序列化C#类的美好生活,而无需自己做JSON。 Node wants me to do the JSON myself which I am having a bad time with. Node希望我自己做JSON,但我遇到了麻烦。

It's a fitness app. 这是一个健身应用程序。 You have clients with some progress containing their lifts and stuff. 您的客户有所进步,其中包括他们的升降机和其他东西。 In C#, that is in a Workouts object, as seen below: 在C#中,它位于“锻炼”对象中,如下所示:

    public struct Progress
    {
        public Workout[] Workouts;
    }

Workouts is a re-usable object containing things like timing, sets, repetitions, all the good stuff. 锻炼是一个可重复使用的对象,其中包含计时,设定,重复等所有好东西。 I could just re-write the Workouts object in every single one of my schemas that need it, but there must be a better way? 我可以在需要它的每个模式中重新编写Workouts对象,但是必须有更好的方法吗? I read something about $schema and $ref but am unfamiliar with how to use them or if this is the right application of those keywords. 我读了一些有关$schema$ref但是不熟悉如何使用它们,或者这是否是这些关键字的正确应用。

Lastly, how would I define an array of this custom Workout object? 最后,我将如何定义此自定义Workout对象的数组?

This is my JSON object and the contents of client.js so far. 到目前为止,这是我的JSON对象和client.js的内容。 The progress bit is the thing that I'd ideally like to achieve. 进度位是我理想中想要实现的目标。

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ClientSchema = new Schema({
    measurements: [{
        height: Number,
        weight: Number
    }],
    personal_information: [{
        birthday: Date,
        gender: String
    }],
    contact_information: [{
        phone: String,
        email: String
    }],
    progress: [{
        workouts: WorkoutList
    }]
});

If you just want a common data structure that can be used in various other models then you can create a sub-doc schema (that may or may never have its own model) that will be used by various other schemas. 如果您只希望可以在其他各种模型中使用的通用数据结构,则可以创建一个子文档模式 (该模型可能具有也可能永远没有其自己的模型),该子文档模式将被其他各种模式使用。

NOTE: This provides a singleton schema object which means if you were to modify it one model it would be propagated to any other models that later use it. 注意:这提供了一个单例模式对象,这意味着如果您要修改一个模型,它将被传播到以后使用它的任何其他模型。 If that is a concern than you can convert workout-list-schema.js to be a factory function that would return a new schema object each time. 如果这是一个问题,则可以将workout-list-schema.js转换为工厂函数,该函数每次都会返回一个新的架构对象。

workout-list-schema.js training-list-schema.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var WorkoutListSchema = module.exports.WorkoutListSchema = new Schema({
  // some schema
});

client-schema.js client-schema.js

var mongoose = require('mongoose');
var WorkoutListSchema = require('./workout-list-schema').WorkoutListSchema;
var Schema = mongoose.Schema;

var ClientSchema = new Schema({
  measurements: [{
    height: Number,
    weight: Number
  }],
  personal_information: [{
    birthday: Date,
    gender: String
  }],
  contact_information: [{
    phone: String,
    email: String
  }],
  progress: [{
    workouts: WorkoutListSchema
  }]
});

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

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