所以,我刚刚开始使用 Node/Mongo db 和 typescript,并且我有点陷入为这个嵌套 JSON 数据库创建架构的困境。我已经开始了一些事情,我想知道我是否走在正确的轨道上。所以,如果有人能帮助我,那就太好了。提前致谢
数据
[{
"a_class":[
{
"brand":"A-class",
"id":"1",
"year":"2015"
"price":"12665"
...
"engine_spec":{
...
}
...
}]
}
]
接口(interface)
import { Document } from 'mongoose';
export default interface ICars extends Document {
a_class: BrandList[],
}
interface BrandList {
brand: string;
id:number;
year:number;
main_image: string;
price:number;
transmission:string;
fuel_type:string;
seating_capacity:number;
engine:number;
engine_specs:Specs;
volume_weights:Volume;
performance: Performance
images_interior: Interior
images_exterior: Exterior
}
interface Specs {
power:number;
power_per_litre:number;
torque:number;
fuel_system:number
}
interface Volume {
max_weights:number;
fuel_tank:number
}
interface Performance {
acceleration:number;
maximum_speed:number;
fuel_urban:number;
fuel_extra_urban:number
fuel_combined:number
}
interface Interior {
image_one:string;
image_two:string;
image_three:string;
image_four:string;
}
interface Exterior {
image_one:string;
image_two:string;
image_three:string;
image_four:string;
}
架构
import mongoose, { Schema } from 'mongoose';
import ICars from '../interfaces/cars'
const CarsSchema: Schema = new Schema({
a_class:Array,
brand: String,
year:Number,
})
export default mongoose.model<ICars>('Cars', CarsSchema);
获取路线,所以当我尝试通过postman获取所有数据时,它不允许我,它说找不到路线404。我已将其导入到server.js import carsRouter来自“./routes/cars”; router.use('./cars',carsRouter )
,我不知道错误可能出在哪里
import express from 'express'
import Cars from '../models/cars'
const router = express();
router.get('/cars', (req:any, res:any )=>{
Cars.find()
.then(car=> res.json(car))
.catch(err => res.status(400).json(`Error: ${err}`))
})
export=router
最佳答案
根据 mongoose 文档,我们应该避免扩展 mongoose Document
。
This approach works, but we recommend your document interface not extend Document. Using extends Document makes it difficult for Mongoose to infer which properties are present on query filters, lean documents, and other cases.
We recommend your document interface contain the properties defined in your schema and line up with what your documents look like in MongoDB. Although you can add instance methods to your document interface, we do not recommend doing so.
更多信息请引用:https://mongoosejs.com/docs/typescript.html#using-extends-document
而且看来我们可以使用 mongoose 子文档来满足这个需求。更多子文档信息请引用:https://mongoosejs.com/docs/subdocs.html#subdocuments
因此在这种情况下,我们可以稍微改写一下您的代码:
接口(interface):
export default interface ICars {
a_class: BrandList[],
}
// The other interfaces can be kept
架构:
const Exterior = new Schema({
// own fields
});
const Interior = new Schema({
// own fields
});
const Performance = new Schema({
// own fields
});
const Volume = new Schema({
// own fields
});
const SpecsSchema = new Schema({
// own fields
});
const BrandListSchema = new Schema({
// other fields
engine_specs: [SpecsSchema],
volume_weights: [VolumeSchema],
performance: [PerformanceSchema],
images_interior: [InteriorSchema],
images_exterior: [ExteriorSchema],
});
const CarsSchema: Schema = new Schema({
a_class: [BrandListSchema],
});
关于javascript - 在 Node/MongoDb 中创建架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68018078/