猫鼬模式的角度接口

时间:2020-04-18 17:33:24

标签: angular mongodb mongoose interface schema

请帮助弄清楚如何从数据库模型制作正确的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
            }
        ]
    }
}

1 个答案:

答案 0 :(得分: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.