Node.js POST 请求发送的对象数组不仅是 Object [英] Node.js POST request send array of objects not only Object
问题描述
正如您在图片中看到的那样,它没有任何保存.POST 请求正在工作,但我发送到后端的数据仅保存带有 ID 的新对象,而不保存我提供的数据,我的意思是对象数组.
As you can see in the picture it saves nothing. The POST request is working, but the data which I am sending to Backend it is saving only new Object with ID but not the data which I have giving, I mean Array of Objects.
我没有任何错误或状态正常.我正在用 Postman 测试这个.这是 Postman Post Request
I do not have any error or something the status is OK. I am testing this with Postman. This is the Postman Post Request
我会添加一些代码.
这是模型.
const mongoose = require("mongoose");
const dataModelScheme = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
personalData: [{
_id: mongoose.Schema.Types.ObjectId,
title: String,
firstName: String,
lastName: String,
email: String,
birthday: String,
telephone: Number,
driveLicense: String,
status: Number,
employmentType: String,
job: String,
}],
career: [{
_id: mongoose.Schema.Types.ObjectId,
name: String,
startDate: Number,
endDate: Number,
description: String,
}],
education: [{
_id: mongoose.Schema.Types.ObjectId,
name: String,
description: String
}],
skills: [{
_id: mongoose.Schema.Types.ObjectId,
name: String,
description: String
}],
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
});
module.exports = mongoose.model('ModelSchema', dataModelScheme);
这是控制器.
async store(req, res) {
const { user_id } = req.headers;
const user = await User.findById(user_id);
if (!user) {
return res.status(400).json({ error: 'User does not exist'});
}
const modelData = new ModelData({
_id: new mongoose.Types.ObjectId(),
user: user_id,
personalData: [{
_id: new mongoose.Types.ObjectId(),
title: req.body.personalData.title,
firstName: req.body.personalData.firstName,
lastName: req.body.personalData.lastName,
email: req.body.personalData.email,
birthday: req.body.personalData.birthday,
telephone: req.body.personalData.telephone,
driveLicense: req.body.personalData.driveLicense,
status: req.body.personalData.status,
employmentType: req.body.personalData.employmentType,
job: req.body.personalData.job,
}],
skills: [{
_id: new mongoose.Types.ObjectId(),
name: req.body.skills.name,
description: req.body.skills.description,
}],
education: [{
_id: new mongoose.Types.ObjectId(),
name: req.body.education.name,
description: req.body.education.description,
}],
});
modelData.save().then(result => {
res.status(201).json(result);
console.log(req.body.personalData, "req.body");
console.log(result, "result");
}).catch(error => {
res.status(500).json({error: error});
});
},
推荐答案
这是因为在创建模型时,您只传递了一个用于个人数据、技能和教育的元素.尝试这样的事情
This is because you are passing only one element for personalData, skills and education when you are creating your model. Try with something like this
const modelData = new ModelData({
_id: new mongoose.Types.ObjectId(),
user: user_id,
personalData: req.body.personalData.map(personalData => {
return {
_id: new mongoose.Types.ObjectId(),
title: personalData.title,
firstName: personalData.firstName,
lastName: personalData.lastName,
email: personalData.email,
birthday: personalData.birthday,
telephone: personalData.telephone,
driveLicense: personalData.driveLicense,
status: personalData.status,
employmentType: personalData.employmentType,
job: personalData.job
};
}),
skills: req.body.skills.map(skill => {
return {
_id: new mongoose.Types.ObjectId(),
name: skill.name,
description: skill.description
};
},
education: req.body.education.map(education => {
return {
_id: new mongoose.Types.ObjectId(),
name: education.name,
description: education.description
};
}
});
这篇关于Node.js POST 请求发送的对象数组不仅是 Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!