I tried to update and push the Lowongan array,
but every time I made a patch request only new_id was created in DB,
There's no error in the console only shows the successfully saved data but in DB it's only keys _id
我尝试更新和推送Lowongan数组,但每次我发出补丁请求时,在数据库中只创建了new_id,控制台中没有错误,只显示了成功保存的数据,但在数据库中只显示了key_id
I have tried changing the data request, and look for the schema type
我已经尝试更改数据请求,并查找模式类型
>
>
Please I need help
拜托,我需要帮助
//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 });
}
}
}
更多回答
Why findOneAndUpdate
and .save()
? You may want to use returnOriginal: false
in the findOneAndUpdate
.
为什么使用findOneAndUpdate和.save()?您可能希望在findOneAndUpdate中使用rens Original:False。
after reading mongoose docs abaout subSchema
it seems i applied it wrong in my employer_postSchema i applied it to be : [{{{data}}]
在阅读了关于子模式的Mongoose文档后,我似乎在我的Employer_postSchema中错误地应用了它:[{data}}]
so i change it to be :
因此,我将其更改为:
employerpostschema : [subchema]
and change this code in api handler
并在API处理程序中更改此代码
let cari = await employer.findOneAndUpdate(
{ _id: user },
{
$push: { lowongan: data }, // data without curly bracket
}
更多回答
我是一名优秀的程序员,十分优秀!