TypescriptでMongooseモデルを実装しようとしています。 Googleを精練してみると、ハイブリッドアプローチ(JSとTSを組み合わせる)だけが明らかになりました。 JSなしで私のむしろ素朴なアプローチでUserクラスを実装する方法は?MongooseのTypescriptの方法...?
手荷物なしでIUserModelできるようにしたい。ここで
import {IUser} from './user.ts';
import {Document, Schema, Model} from 'mongoose';
// mixing in a couple of interfaces
interface IUserDocument extends IUser, Document {}
// mongoose, why oh why '[String]'
// TODO: investigate out why mongoose needs its own data types
let userSchema: Schema = new Schema({
userName : String,
password : String,
firstName : String,
lastName : String,
email : String,
activated : Boolean,
roles : [String]
});
// interface we want to code to?
export interface IUserModel extends Model<IUserDocument> {/* any custom methods here */}
// stumped here
export class User {
constructor() {}
}
'User'はクラスを作成できません。これは、非同期操作であるためです。あなたが 'User.create({{}}) 'と呼ぶ必要があるので、約束を返さなければなりません。 – Louy
具体的には、OPのコードで与えられているように、 'User'がクラスになれない理由を詳しく教えてください。 –
代わりにhttps://github.com/typeorm/typeormをお試しください。 – Erich