[英]Post data to Subdocument - Mongoose Express
谷歌搜索后......
使用 Mongoose/Express 保存數據我有問題:無法將數據發布到子文檔或嵌套文檔
這是我的 Mongoose model:
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"
}
]
這是我收到的數據,但實際上無法保存:
{
email: 'manana04@gmail.com',
password: '$2a$10$DSpWahC5QCDEWwr1VljOXeok3awr/vVzDRcGazQsevu3H1ZgNY.pm',
firstname: 'dkfsdgk',
lastname: 'kvhdvgdjk',
job_title: 'cbhckvk5',
description: 'sdfvbilfgad',
hourly_rate: '454',
city: 'sdkvhdfvik',
country: 'hsvhdo',
education: [],
experience: [
{
title: 'Title experincev v',
company: 'dfvbhdfighfi',
date_bg: '2021-02-04',
date_end: '2021-02-17',
description: 'sdfsfsdfs'
},
{
title: 'tititititititititititititit',
company: 'dfldhkfjshdkfhsdkfshk',
date_bg: '2021-02-04',
date_end: '2021-02-16',
description: 'sdfshfdslfsdhfhs'
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.