node.js - 将数据发布到子文档 - Mongoose Express
问题描述
谷歌搜索后!...
使用 Mongoose/Express 保存数据我有问题: 无法将数据发布到子文档或嵌套文档
这是我的猫鼬模型:
const Users = new Schema({
firstname : {
type : String
},
password : {type : String},
phone : {type : Number},
city : {type : String},
country : {type : String},
experience : [
{
title : {type : String},
company : {type : String},
description : {type : String},
date_bg : {type : Date},
date_end : {type : Date},
}
],
education : [
{
degree : {type : String},
school : {type : String},
description : {type : String},
date_bg : {type : Date},
date_end : {type : Date},
}
],
// ...
})
这是邮政路由器:
const addUser = async (req, res) => {
// my data from fronted is like this :
// req.body.experience is like this (same as education):
// [{title: "title experince 1", company: "title experince", date_bg: "2021-02-11", date_end: "2021-02-02", description: "title experince"}
{title: "title experince 2", company: "title experince 2", date_bg: "2021-02-10", date_end: "2021-02-08", description: "tstset"}]
const user = new Users(req.body) // This cause cast error for the *experience* and *education*
try {
await user.save()
return res.status(200).json({message : "Signin up successfully, Your account is now active!"})
} catch (error) {
return res.json({error : "Something went wrong, please try again later!", message : error})
}
}
我的问题是:
如何将我的数据发布到子文档?(我的问题在于经验和教育模式)
解决方案
在 request 的 req.body.experience 中,1
在第二次体验之前有一个,所以改变 body 的格式是这样的:
[
{
title: "title experince 1",
company: "title experince",
date_bg: "2021-02-11",
date_end: "2021-02-02",
description: "title experince"
},
{
title: "title experince 2",
company: "title experince 2",
date_bg: "2021-02-10",
date_end: "2021-02-08",
description: "tstset"
}
]
推荐阅读
- c++ - Microchip SAM R34 上的串行
- javascript - 如何发送包含空格和多个字符的文件
- javascript - Django 呈现一个“异常”页面,而不是一个可以帮助调试的页面
- python - Python中的递归、多态和继承混淆
- python - 刷新成员计数 - Discord.py
- riscv32 - risc-v neg 指令是如何实现的?
- javascript - 如何对我的列表进行排序,以便在下拉列表中有升序和降序选项?
- try-catch - 在 TCL catch 命令中捕获标准输出
- python - AttributeError:“功能”对象没有属性“配置”
- python - Python:确保字符串是用户的有效输入,并拒绝任何其他输入