我似乎无法弄清楚为什么数据库不会更新其文档,这是我写的代码试图更新这些文档:
if(didChange == true || didChange2 == true) {
Report.find({}, function(err, report) {
report.forEach(function (rep) {
if(String(rep.author[0]) == String(id)) {
console.log("IF HIT");
rep.authors[0] = req.body.firstName + " " + req.body.lastName;
rep.save(function(err) {
if (!err) {
console.log("Success");
}
else {
console.log(err);
}
});
console.log(rep);
}
tracker = tracker+1;
if(tracker == report.length){
console.log(tracker);
console.log("finish");
res.json(req.body);
}
});
});
}
else {
console.log("Hit");
res.end();
}
从控制台显示authors字段是新值,但不保存。从db命令shell查看db时,实际上没有更新任何值。我有点卡在哪里去。我确定它与异步以及我如何编写它或相应地更新多个文档有关。
控制台输出:
ID: 55ad14d0c2419660021d80b3
IF HIT
{ _id: 55ba25e303be4c4c0edcd617,
body: 'jajjajajaja',
owner: 55ae608c5562285c11af5ad4,
date: 'Thu Jul 30 2015 09:25:55 GMT-0400 (Eastern Daylight Time)',
title: 'subs sub',
__v: 1,
parentReport: 55ba1f4226945e8014f0fcdf,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55ba27b6403852900eec3ee5,
body: 'asdadsa',
owner: 55ae608c5562285c11af5ad4,
date: 'Thu Jul 30 2015 09:33:42 GMT-0400 (Eastern Daylight Time)',
title: 'for u deff',
__v: 1,
parentReport: 55ba1f4226945e8014f0fcdf,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55ba283d403852900eec3ee6,
body: 'adre',
owner: 55ae608c5562285c11af5ad4,
date: 'Thu Jul 30 2015 09:35:57 GMT-0400 (Eastern Daylight Time)',
title: 'da',
__v: 1,
parentReport: 55ba1f4226945e8014f0fcdf,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55ba2c140d63028c14810706,
body: 'Enter subreport here',
owner: 55ae608c5562285c11af5ad4,
date: 'Thu Jul 30 2015 09:52:20 GMT-0400 (Eastern Daylight Time)',
title: 'tstststst',
__v: 1,
parentReport: 55ba1f4226945e8014f0fcdf,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55ba2c250d63028c14810707,
body: 'ajjajaj',
owner: 55ae608c5562285c11af5ad4,
date: 'Thu Jul 30 2015 09:52:37 GMT-0400 (Eastern Daylight Time)',
title: 'jajajajaj',
__v: 1,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55bfc2784bb0ce6c1b41bdb2,
body: '1231321',
owner: 55bf60ccf5b98cd404e39eca,
date: 'Mon Aug 03 2015 15:35:20 GMT-0400 (Eastern Daylight Time)',
title: 'depth4',
__v: 1,
parentReport: 55bfc1a533c03294168cf07f,
isCompleted: true,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55c9f8d8e87a23080470e8b7,
body: 'fefefeport here',
owner: 55c9f80cd951be3c129f9a9f,
date: 'Tue Aug 11 2015 09:30:00 GMT-0400 (Eastern Daylight Time)',
title: 'ffffefefe',
__v: 1,
parentReport: 55c9f8bee87a23080470e8b6,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
IF HIT
{ _id: 55c9fe5ae9be72d0196f458f,
owner: 55ad14d0c2419660021d80b3,
title: 'fafafaf',
date: 'Tue Aug 11 2015 09:53:30 GMT-0400 (Eastern Daylight Time)',
body: 'afafafafaf',
__v: 0,
isCompleted: false,
subreport: [],
authors: [ 'deff feed' ],
author: [ 55ad14d0c2419660021d80b3 ] }
67
finish
Success
Success
Success
Success
Success
Success
Success
Success
Mongoose Debug:
Mongoose: users.ensureIndex({ email: 1 }) { unique: true, background: true, safe
: undefined }
Succeeded and connected to: mongodb://localhost/UserReports
Mongoose: users.findOne({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { fields:
undefined }
undefined
Map {}
Mongoose: users.findOne({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { fields:
undefined }
Warning: No route matches path "/assets/styles/public/assets/sass.map". Make sur
e you have <Route path="/assets/styles/public/assets/sass.map"> somewhere in you
r routes
Mongoose: users.findOne({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { fields:
undefined }
Mongoose: users.findOne({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { fields:
undefined }
Mongoose: users.findOne({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { fields:
undefined }
Mongoose: users.findOne({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { fields:
undefined }
Mongoose: reports.find({}) { fields: undefined }
ID: 55ad14d0c2419660021d80b3
Mongoose: users.update({ _id: ObjectId("55ad14d0c2419660021d80b3") }) { '$set':
{ 'profile.firstName': 'deff' } }
答案 0 :(得分:1)
我建议你使用例如async modules "each" method循环遍历报表数组。
使用此方法,您可以逐个遍历报表,并且只进行下一次迭代,例如, save
回调已完成。
这也使您的代码更具可读性和可维护性。
然后,查看Mongoose Docs FAQ(第一个):
Mongoose不会为数组索引创建getter / setter
所以你应该做
var newAuthorName = req.body.firstName + " " + req.body.lastName
doc.authors.set(0, newAuthorName);
而不是
var newAuthorName = req.body.firstName + " " + req.body.lastName
doc.authors[0] = newAuthorName;