我正在使用Node,Express,Mongoose和Vue.js向Mongodb发布项目。该项目是一个混合对象和数组。对象发布将数组发布继到Mongo之后,并创建一个ID,但不会发布数据。
猫鼬模式为
const ReportSchema = Schema(
{
month: String,
projects:
[
{
code: String,
name: String,
staff: String,
support: String
} // it was missing here
],
API是
app.post('/api/report/create', (req, res) => {
const report = new Report({
month: req.body.month,
projects:
[
{
code: req.body.projects.code,
name: req.body.projects.name,
staff: req.body.projects.staff,
support: req.body.projects.support
}
],
Vue.js方法是
methods: {
create(){
let data = {
month: this.month,
projects: [
{
code: this.projects.code,
name: this.projects.name,
staff: this.projects.staff,
support: this.projects.supported
}
],
当我在Postman中执行发布请求时,对象的返回是可以的,但是数组没有发布数据。
"report": {
"_id": "5c91b6d449f21705a0270732",
"month": "January",
"projects": [
{
"_id": "5c91b6d449f21705a0270733"
}
],
"__v": 0
}
感谢您的帮助!
答案 0 :(得分:0)
在您的创建方法中,
let data = {
month: this.month,
projects: [
{
code: this.projects.code,
name: this.projects.name,
staff: this.projects.staff,
support: this.projects.supported
}
],
您将项目作为数组发送,并在API中作为对象访问它--->
req.body.projects.code
在您的API中进行以下更改,它应该可以工作。
app.post('/api/report/create', (req, res) => {
const report = new Report({
month: req.body.month,
projects: req.body.projects
})