2016-12-11 10 views
0

私はmongoose TypeScriptモデルに次のコードを使用しました。今mongoose typings、idプロパティが利用できません

import mongoose = require('mongoose') 

export interface PieceInterface extends mongoose.Document { 
    date: Date 
    summary: string 
    source: string 
    link: string 
} 

export const PieceSchema = new mongoose.Schema({ 
    date: { type: Date, default: new Date(), required: true }, 
    summary: { type: String, default: 'summary', required: true }, 
    source: { type: String, default: 'source', required: true }, 
    link: { type: String, default: '#', required: true } 
}) 

export const Piece = mongoose.model<PieceInterface>('Piece', PieceSchema) 

、私はPiece.find({}).then(x => {...})を使用し、x予想通りPromise<PieceInterface[]>です。

しかし、私は

Piece.find({}).then(x => { 
    console.log(x[0].id) 
}) 

を正しくコンパイルするために取得することはできませんよ、それは常に私にerror TS2339: Property 'id' does not exist on type 'PieceInterface'を伝えます。

PieceInterfaceは、私はそれは、

interface MongooseDocumentOptionals { 
    id?: string; 
} 

そして、私の心の中で、

interface Document extends MongooseDocument, NodeJS.EventEmitter, ModelProperties 

そして、

class MongooseDocument implements MongooseDocumentOptionals 

そして最後に、この...べきで見て、mongoose.Documentを拡張し、以来作業。 MongooseDocumentModelPropertiesで提供される変数を使用できますが、MongooseDocumentOptionalsでは使用できません。

何か不足していますか?

参考文献:あなたが行う必要がありますので、モンゴにおけるIDのためのGitHub から mongoose.d.ts

Definition of document

Definition of MongooseDocument

Definition of MongooseDocumentOptionals

答えて

0

デフォルトのフィールド名は、_ididではありません。

Piece.find({}).then(x => { 
    console.log(x[0]._id) 
}) 
+0

しかし、モンゴーデフォルトでは仮想の 'id'ゲッタを持っているので、' MongooseDocumentOptional'の型定義に 'id'が含まれていると思います。 –

関連する問題