javascript - Mongoose/Mongo 和保存

标签 javascript node.js mongodb mongoose

我似乎无法弄清楚为什么数据库不会更新其文档,这是我编写的尝试更新这些文档的代码:

    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 查看数据库时,实际上没有更新任何值。我有点不知道该去哪里。我确信它与异步以及我如何编写它或相应地更新多个文档有关。

控制台输出:

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 调试:

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' } }

最佳答案

我建议您使用例如async modules "each" method循环访问您的报告数组。

使用此方法,您可以逐个循环浏览报告,并且仅进行下一次迭代,例如save 回调已完成。 这也使您的代码更具可读性和可维护性。

然后,查看 Mongoose Docs FAQ (第一个):

Mongoose doesn't create getters/setters for array indexes

所以你应该这样做

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;

关于javascript - Mongoose/Mongo 和保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31946149/

相关文章:

node.js - 在不同文件中为 Mongoose 需要另一个模型中的模型模式

node.js - Nodejs 如何知道同步还是异步

javascript - Supertest 中的负面断言

mongodb - 使用 := gives unused error but using = don't in Go

c# - MongoDB 序列化 Dictionary<MyEnum,object>

javascript - 按Tab键时如何打开选择框?

javascript - javascript中的 header 位置

javascript - 当我们使用 join 方法时,根据根表的 id 进行排序

javascript - 检查脚本被调用的位置/哪一行或文档

node.js - 具有不同库存的用户的 MongoDB 数据结构