好的,这是我的模特:
var Releases = new Schema({
status: String,
title: String,
type: String,
url: String,
text: String,
tracks: Number,
length: String,
links: [{ url: String, title: String }],
images: [{ small: String, big: String, full: String }],
music: {
musician: String,
album: String,
albumTrack: Number,
duration: Number,
releaseDate: Date,
song: String,
songDisc: Number,
songTrack: Number
},
twitter: {
card: String,
title: String,
site: String
},
og: {
title: String,
type: String,
url: String,
siteName: String
}
});
当用户发布帖子请求时,我想将其保存到数据库中,这是一个新实例的制作:
var releases = new Releases({
status: req.body.status,
title: req.body.title,
type: req.body.type,
url: req.body.url,
text: req.body.text,
tracks: req.body.tracks,
length: req.body.length,
links: [],
images: [],
music: {
musician: req.body.music.musician,
album: req.body.music.album,
albumTrack: req.body.music.albumTrack,
duration: req.body.music.duration,
releaseDate: req.body.music.releaseDate,
song: req.body.music.song,
songDisc: req.body.music.songDisc,
songTrack: req.body.music.songTrack
},
twitter: {
card: req.body.twitter.card,
title: req.body.twitter.title,
site: req.body.twitter.site
}
og: {
title: req.body.og.title,
type: req.body.og.type,
url: req.body.og.url,
siteName: req.body.og.siteName
},
})
//push links
for (var i = 0; i < req.body.links.length; i++) {
releases.links.push({ url: req.body.links[i].url, title: req.body.links[i].title })
}
//push images
for (var i = 0; i < req.body.images.length; i++) {
releases.images.push({ small: req.body.images[i].small, big: req.body.images[i].big, full: req.body.images[i].full })
}
一切正常,除了“og”对象,当我控制日志时,og对象根本不会显示。
我进一步调查了这个问题并意识到该对象被认为是一个字符串。或者至少这就是我的想法。
如果我控制台登录模型,这就是它的样子:
'twitter.title':
SchemaString {
enumValues: [],
regExp: null,
path: 'twitter.title',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
'twitter.site':
SchemaString {
enumValues: [],
regExp: null,
path: 'twitter.site',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
og:
SchemaString {
enumValues: [],
regExp: null,
path: 'og',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
对我而言,og对象看起来像是一个字符串,但其他两个对象被视为对象。
我做错了什么吗? 任何其他反馈当然都很受欢迎,我对这一切都不熟悉。
谢谢!