我尝试更新并推送 Lowongan 数组,
但是每次我发出补丁请求时,数据库中都只创建了 new_id,
控制台中没有错误,仅显示成功保存的数据,但在数据库中只有键 _id
我尝试更改数据请求,并查找架构类型
请我需要帮助
//subschema
company_postSchema = new Schema({
job_post_title: { type: String, required: true },
number_of_vacancy: { type: Number, required: true },
commitment_type: { type: String, required: true },
salary: { type: String, required: true },
location: { type: String, required: true },
remote_job: { type: Boolean, required: true },
visa: { type: Boolean, required: true },
forreignn_applicant: { type: Boolean, required: true },
visa_sponsorship: { type: Boolean, required: true },
job_description: { type: String, required: true },
});
//parent schema
const employerSchema = new Schema(
{
account_type: { type: String },
company_name: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
phone_number: { type: String, required: true },
company_website: { type: String, required: true },
company_country: {
label: { type: String, required: true },
value: { type: String, required: true },
},
company_city: { type: String, required: true },
company_address: { type: String, required: true },
company_logo: { type: String },
lowongan: [{ company_postSchema }],
},
{ timestamps: true }
);
//new post lowongan request
{
"job_post_title":"dsadas" ,
"number_of_vacancy": 2,
"commitment_type":"dasdasd",
"salary": "20000",
"location": "jakarta",
"remote_job": true,
"visa": true,
"forreignn_applicant": false,
"visa_sponsorship": false,
"job_description": "dadadasdadsadadad"
}
//API request handle
export default async function company_acc(req, res) {
let connect = await connectToDB(); //connect DB are ok
if (req.method === "PATCH") {
let data = req.body;
console.log(req.body);
let user = "64fc1bf744978576f35a9d72";
try {
let cari = await employer.findOneAndUpdate(
{ _id: user },
{
$push: { lowongan: { data } },
}
);
await cari.save();
res.json(cari);
} catch (error) {
console.log(error);
res.json({ err: error.message });
}
}
}
在阅读了关于 subSchema 的 mongoose 文档后
看来我在雇主帖子架构中应用了错误,我将其应用为:[{{{data}}]
所以我将其更改为:
employerpostschema : [subchema]
并在 api 处理程序中更改此代码
let cari = await employer.findOneAndUpdate(
{ _id: user },
{
$push: { lowongan: data }, // data without curly bracket
}