2017-07-02 13 views
1

私のコレクションに挿入しようとしていますが、挿入しますが、サブ文書は保存されていません。マングースが子の文書を保存していません

私はこのスキーム/モデル持っている:私は、私が保存すると

import * as bodyParser from 'body-parser' 
app.use(bodyParser.json()) 

app.post('/save/:type', async (req, res) => { 
    if (!req.xhr) res.sendStatus(400) 
    let p = new Person({ 
    name: { 
     first: req.body.first, 
     last: req.body.last 
    }, 
    dob: new Date() 
    }) 
    p.save((err, data) => { 
    if (err) console.log(err); 
    else console.log('Saved : ', data); 
    }) 
    res.sendStatus(200) 
}) 

:私は、データを渡すと、そのようなモデルを使用することを明示使用しています

import { Schema, Document, Model, model } from 'mongoose' 

export interface IPerson { 
    name: { 
    first: string 
    last: string 
    } 
    dob: Date 
} 

export interface IPersonModel extends IPerson, Document { } 

let nameSchema: Schema = new Schema({ 
    first: String, 
    last: String 
}) 
let PersonName = model('PersonName', nameSchema) 

export var personSchema: Schema = new Schema({ 
    name: { child: PersonName.schema }, 
    dob: Date 
}, { timestamps: true }) 

export const Person: Model<IPersonModel> = model<IPersonModel>('Person', personSchema, 'people') 

を端末に次の出力を取得します。

Saved : { __v: 0, 
    updatedAt: 2017-07-02T14:52:18.286Z, 
    createdAt: 2017-07-02T14:52:18.286Z, 
    dob: 2017-07-02T14:52:18.272Z, 
    _id: 595908a27de708401b107e4b 
} 

なぜ、私の子供nameは保存されないのですか?

答えて

0

わかりましたので、私は何を必要とするので、同じようtypechildを変更した:

name: { child: PersonName.schema } 

name: { type: PersonName.schema } 

にスキーマを渡すことによって、これも_idを作成するので、私はできましたスキーマを渡す代わりにそれを変更するには、次のようにオブジェクトを渡します。

関連する問題