Post 对象和数组混合的项目 Mongo - 数组没有 post 数据
Post item with mix of objects and arrays to Mongo - the array does not post data
我正在使用 Node、Express、Mongoose 和 Vue.js posting 和项目到 Mongodb。该项目是混合对象和数组。对象 post 继承数组 post 到 Mongo 并创建一个 ID,但数据未被 post 编辑。
Mongoose 模式是
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 中执行 post 请求时,对象 return 可以,但数组不是 posting 数据。
"report": {
"_id": "5c91b6d449f21705a0270732",
"month": "January",
"projects": [
{
"_id": "5c91b6d449f21705a0270733"
}
],
"__v": 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
})
我正在使用 Node、Express、Mongoose 和 Vue.js posting 和项目到 Mongodb。该项目是混合对象和数组。对象 post 继承数组 post 到 Mongo 并创建一个 ID,但数据未被 post 编辑。
Mongoose 模式是
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 中执行 post 请求时,对象 return 可以,但数组不是 posting 数据。
"report": {
"_id": "5c91b6d449f21705a0270732",
"month": "January",
"projects": [
{
"_id": "5c91b6d449f21705a0270733"
}
],
"__v": 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
})