【问题标题】:Angular interface from mongoose schema来自猫鼬模式的角度接口
【发布时间】:2020-08-01 05:48:46
【问题描述】:

请帮助弄清楚如何从 db 模型制作正确的 Angular 接口

在后端有以下架构:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const incomeSchema = new Schema({   
    income: {
        type: Number
    },
    incomeDate: {
        type: Date,
        default: Date.now
    }
})


const userSchema = new Schema({
    username :{
        type: String,        
    },
    website: {
        sitename: {
            type :String
        },        
        income: [incomeSchema]
    }    
})
module.exports = mongoose.model('User', userSchema)

这是编写 Angular 接口的正确方法吗?不能让它工作:(

export class userInterface  {
    _id: String
    username: String
    website: {
        sitename: String,
        income: [
            {
                income : Number
            }
        ]
    }
}

【问题讨论】:

    标签: angular mongodb mongoose interface schema


    【解决方案1】:

    类不适合声明表示 HTTP 响应的类型,因为 HTTP 请求产生的反序列化 JSON 值永远不会是类的实例。接口是它的完美候选者。

    解决方案

    interface IncomeData{
    income:number;
    incomeDate:Date;
    }
    
    interface Website{
    sitename: string;
    incomeData:Array<IncomData>;
    }
    
    export interface User{
        id: string;
        username: string;
        website:Website;
    }
    

    How to design a Typescript Model for Response Returned By HttpClient library in Angular.

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 2014-11-18
      • 2018-01-21
      • 2020-06-29
      • 2020-06-21
      • 2016-08-05
      • 1970-01-01
      • 2015-09-26
      • 2020-08-03
      相关资源
      最近更新 更多