这是我的代码:
router.post('/update-posting', (req, res, next) => {
Account.findById(req.user._id)
.then(doc => {
var type = [];
if (req.body.full !== undefined) {
type.push('full');
}
if (req.body.part !== undefined) {
type.push('part');
}
if (req.body.seasonal !== undefined) {
type.push('seasonal');
}
if (req.body.temporary !== undefined) {
type.push('temp');
}
var title = req.body.title;
var salary = req.body.salary;
var timeline = req.body.timeline;
var experience = req.body.experience;
var description = req.body.description;
var duties = req.body.duties;
doc.postings[req.body._id] = {
_id: req.body._id,
title: title,
type: type,
salary: salary,
timeline: timeline,
description: description,
duties: duties,
experience: experience,
};
doc.save(r=>console.log(r));
})
.then(() => res.redirect('/employer/booth-edit'))
.catch(e => console.log(e))
});
这是模特:
var mongoose = require('mongoose');
var plm = require('passport-local-mongoose');
var accountSchema = new mongoose.Schema({
// username (comes with passport): email; -> just for reference.
accType: String,
fullName: String,
displayName: String,
companyName: String,
contactPersonFullName: String,
companyWebsite: String,
city: String,
province: String,
postalCode: String,
phoneNumber: String,
hiringRegion: [], // TODO
description: String,
logo: [],
workingWithEOESC: Boolean,
industry: String,
phone: String,
ageGroup: String,
education: String,
lookingForWork: String,
employmentStatus: String,
resume: [],
mainWorkExp: String,
boothVisits: Number,
postings: []
});
accountSchema.plugin(plm);
module.exports = mongoose.model('account', accountSchema);
我正在做的是尝试更新postss数组中的对象。现在这里是奇怪的部分。当我在 doc.save()
之前控制台记录结果 时,我得到了更新版本......当我在控制台上记录来自doc.save()
的响应时,我得到了null ......我确定这是一个小虫子,但我无法在任何地方看到它。
所有字段都来自req.body
对象。
以下是日志。
原始对象:
{ _id: 0,
title: 'Web developer',
type: [ 'full', 'seasonal' ],
salary: '14$',
timeline: '3 months',
description: 'tada',
duties: 'tada',
experience: '5 years' }
更新了对象:
{ _id: '0',
title: 'Car mechanic',
type: [ 'part', 'temp' ],
salary: '50$',
timeline: '2 weeks',
description: 'desc',
duties: 'resp',
experience: '4 years' }
doc.save()
回复:
null
更有趣的是,这是我用于“创建”职位发布的代码。这几乎是相同的代码,它运作得非常好:
router.route('/add-posting')
.get((req, res, next) => {
res.render('users/employer/add-posting', {
title: 'Employer Booth - Add Job Posting',
user: req.user
});
})
.post((req, res, next) => {
// Determining type of work.
var type = [];
if (req.body.full !== undefined) {
type.push('full');
}
if (req.body.part !== undefined) {
type.push('part');
}
if (req.body.seasonal !== undefined) {
type.push('seasonal');
}
if (req.body.temporary !== undefined) {
type.push('temp');
}
var title = req.body.title;
var salary = req.body.salary;
var timeline = req.body.timeline;
var experience = req.body.experience;
var description = req.body.description;
var duties = req.body.duties;
Account.findById(req.user._id)
.then(doc => {
doc.postings.push({
_id: doc.postings.length,
title: title,
type: type,
salary: salary,
timeline: timeline,
description: description,
duties: duties,
experience: experience,
});
doc.save();
})
.then(() => res.redirect('/employer/booth-edit'))
.catch(e => console.log(e));
});
答案 0 :(得分:0)
我记得以前我遇到过这个问题。原来我们需要更新数组:
doc.postings.set(req.body._id, {
_id: req.body._id,
title: title,
type: type,
salary: salary,
timeline: timeline,
description: description,
duties: duties,
experience: experience,
});
我记得在那里读过一个问题。如果我再次找到它,将添加链接。
我们需要使用.set方法而不是=运算符。
答案 1 :(得分:0)
他们这样做可能会有效。但是mongo已经为您提供了update
或findOneAndupdate
方法。我建议你使用它们。
您的查询在需要时很容易理解和调试。
尝试类似
的内容db.collection.update({_id: .user._id},{$push :{postings: yourObject}})