我有一个嵌套对象,我可以正确获取它,但无法将嵌套对象值存储在架构中。以下是我的代码片段:
标头.js路由器文件
router.post('',(req, res)=>{
//console.log(req.body)
const header = new headerModel({
register:{
name: req.body.name,
url: req.body.url
},
logo:{
image: req.body.image,
altText: req.body.altText,
url: req.body.url
}
})
console.log(header)
})
module.exports = router
标头.js架构文件
const mongoose = require('mongoose')
const headerSchema = mongoose.Schema({
register: {
name: {
type: String
},
url: {
type: String
}
},
logo: {
image: {
type: String,
},
altText: {
type: String
},
url: {
type: String
},
}
})
module.exports = mongoose.model('header', headerSchema)
杰伦
{
"register":{
"name":"Register",
"url":"/register"
},
"logo":{
"image":"/imagePath/ab.png",
"alttext":"Home",
"url":"/"
}
}
我需要分别在路由器文件中的注册和登录对象中存储名称和 url 的值
登录控制台时路由器文件中的标头不包括寄存器或徽标
因为您以错误的方式从 JSON 获取数据,并且尚未保存标头。您可以通过以下方式解决它:
let header = new headerModel({
register:{
name: req.body.register.name,
url: req.body.register.url
},
logo:{
image: req.body.logo.image,
altText: req.body.logo.altText,
url: req.body.logo.url
}
})
header.save(function (err, doc) {
// Do some thing you want
})
相关信息可以在这里找到。