Node.js 使用typescript在nodejs中从dto到mongodb实体的数据映射
我来自C#背景,我已经开始学习Node.js和Express。 谁能告诉我如何将我正在使用的作为接口类型的JSON模型映射到MongoDB实体。是否有用于此的映射程序库 这是具有属性familyName的族架构Node.js 使用typescript在nodejs中从dto到mongodb实体的数据映射,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我来自C#背景,我已经开始学习Node.js和Express。 谁能告诉我如何将我正在使用的作为接口类型的JSON模型映射到MongoDB实体。是否有用于此的映射程序库 这是具有属性familyName的族架构 const familySchema = new mongoose.Schema({ familyName: { type: String, required: true, min: 6, max: 255, }, }); 这是我的界面 expo
const familySchema = new mongoose.Schema({
familyName: {
type: String,
required: true,
min: 6,
max: 255,
},
});
这是我的界面
export interface IFamilyViewModel {
id: string;
name: string;
}
当我尝试将findById调用的结果映射到IFamiltViewModel类型时,它会添加额外的字段
import family from "../entities/family";
import { IFamilyViewModel } from "../models/IFamilyViewModel";
import ResponseCreator from "../models/responseCreator";
export class ProductService {
getFamily(id: string) {
return family.findById(id).then((response) => {
var data: IFamilyViewModel = response?.toJSON();
console.log("data is ", data);
return new ResponseCreator(200, data);
});
}
}
这就是我想要的响应格式
"response": {
"id": "5f9706cb62f02abc247fc743",
"name": "T-Shirts",
}
"response": {
"id": "5f9706cb62f02abc247fc743",
"name": "T-Shirts",
}