Karan
Karan

Reputation: 1151

Mongoose/Mongo and Saving

I can't seem to figure out why the database won't update its documents, here is the code I have written trying to update these documents:

    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();
  }

From the console it is showing that the authors field is the new values, however it is not saving. When looking at the db from the db commandshell, none of the values are actually updated. I'm kind of stuck on where to go. I'm sure it has something to do with async and how I wrote it or updating multiple documents accordingly.

Console output:

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

Upvotes: 0

Views: 60

Answers (1)

jukempff
jukempff

Reputation: 965

I'd suggest, you use e.g. the async modules "each" method to loop through your report array.

With this method you can loop through your reports one by one, and only take the next iteration, if e.g. a save callback has been finished. This also makes your code more readable and maintainable.

Then, check out the Mongoose Docs FAQ (the first one):

Mongoose doesn't create getters/setters for array indexes

So you should do

var newAuthorName = req.body.firstName + " " + req.body.lastName
doc.authors.set(0, newAuthorName);

instead of

var newAuthorName = req.body.firstName + " " + req.body.lastName
doc.authors[0] = newAuthorName;

Upvotes: 1

Related Questions