【发布时间】:2016-07-04 13:36:27
【问题描述】:
我正在使用用 TypeScript 编写的 Angular 2 编写一个简单的 Web 应用程序。 MongoDB 是我在 Mongoose 框架上运行的数据库,同时在 Express 框架上的节点服务器上运行。我的 MongoDB 和 Node 代码是用 vanilla JS 编写的。
现在,我为国家创建了一个 Mongoose 模型,如下所示:
"use strict";
const Schema = require('mongoose').Schema,
db = require('../../config/database');
let countrySchema = new Schema({
countryName: { type: String, index : { unique : true } }
});
let Country = db.model('Country', countrySchema);
module.exports = Country;
现在,Country 是我想要的对象。在我的应用组件中,我有:
import { Component } from '@angular/core';
import { CountryService } from '../services/country.service';
import { Country } from '../models/country.model';
@Component({
selector: 'my-app',
templateUrl: 'app/views/app.component.html',
providers: [ CountryService ]
})
export class AppComponent {
originCountries: Country[];
destinationCountries: Country[];
constructor(private countryService: CountryService) { };
ngOnInit() {
this.getCountries();
}
getCountries() {
this.countryService.getCountries()
.then(countries => {
this.originCountries = countries;
this.destinationCountries = countries;
});
}
}
看看 originCountries 和 destinationCountries 应该如何成为国家的数组?我不能只从 Country 模型中导入 Country(尽管当时我的脑海中听起来很正确)。
创建基于 Mongoose 模型的国家/地区类的最佳方法是什么?
【问题讨论】:
-
我对 TypeScript 不是很熟悉,但这样的事情不会做的事:github.com/Appsilon/styleguide/wiki/mongoose-typescript-models ?
标签: angularjs node.js mongodb typescript angular