私は、TypeScriptで書かれたAngular 2を使って簡単なWebアプリケーションを書いています。 MongoDBは、Mongooseフレームワーク上の私のデータベースであり、Expressフレームワーク上のNodeサーバー上で動作します。私のMongoDBとNodeコードはvanilla JSで書かれています。MongooseモデルをTypescriptクラスとして表現する
今、私は次のように国のためにマングースモデルを作成:
"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;
は今、国は、私は私の物になりたいものです。私のアプリコンポーネントでは、私は持っています:
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;
});
}
}
countriesCountriesとdestinationCountriesはどのように国の配列になるのですか?私はカントリーモデルからカントリーをインポートすることはできません(それは私の頭の中で当時のように聞こえましたが)。
モンゴースモデルに基づくカントリークラスを作成する最適な方法は何ですか?
私はTypeScriptについてよく知りませんが、このようなものはありません:https://github.com/Appsilon/styleguide/wiki/mongoose-typescript-models? –