【发布时间】:2017-12-05 22:21:35
【问题描述】:
我试图插入到我的集合中,它确实插入了,但子文档没有被保存,我不知道为什么。
我有这个方案/模型:
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')
我正在使用 express 传递数据并像这样使用 model:
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)
})
当我保存时,我会在终端得到以下输出:
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 没有得救?
【问题讨论】:
标签: javascript mongodb typescript mongoose insert