1

I can't understand why this simple code fails in a very strange way: after calling the mongoose method, I get no messages, and program terminates, without any action on db.
This is the code:

var save = function(trace, callback) {
  console.log('saving...');
  Trace.findOneAndUpdate(
    { // query
      phone: trace.phone,
      link: trace.link,
    },
    { // object to save
      phone: trace.phone,
      title: trace.title,
    },
    { // options
      new: true, // return the modified document rather than the original
      upsert: true, // creates the object if it doesn't exist
      passRawResult: true // pass back mongo row result
    },
    function(err, doc, raw) { // result callback
      if (err) {
        console.log('can\'t save trace', trace.phone, trace.link, ':', err);
      } else {
        if (raw.lastErrorObject.updatedExisting) {
          console.log('trace', doc.phone, doc.link, 'updated');
        } else {
          console.log('trace', doc.phone, doc.link, 'added');
        }
        images.push(doc); // push added image
      }
      callback(err, doc); // finish image save
    }
  );
};

var trace = {
  phone: '3331234567',
  link: 'http://www.example.com/',
  title: 'title 1',
  description: 'descrption 1',
  dateOfLastSync: new Date(),
};

save(trace, function(err, doc) {
  if (err) {
    console.error('err:', err);
  }
  console.log('trace updated:', doc);
});

The output is just:

saving...

Then program terminates, without adding or modifying any data on db.

This is the model, if it can help:

var trace = new mongoose.Schema({
  phone: { type: String, required: true },
  link: String,
  title: String,
  description: String,
  dateOfLastSync: { type: Date, default: Date.now },
});
trace.index({ link: 1 }, { unique: false });
trace.index({ phone: 1 }, { unique: false });
trace.index({ link: 1, phone: 1 }, { unique: true });

module.exports = mongoose.model('Trace', trace);

I'm sure I'm missing something obvious, but... :-(
Any clue?

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • you seem to mix logging to a `log` object and to console - is your `log.info` working and really logging to the console? – Reto Mar 06 '16 at 13:07
  • Yup! You're right... Changed code in question... In my code I use log() from winston logger... Unfortunately, that was not the cause, same behaviour observed... :-( – MarcoS Mar 06 '16 at 13:12
  • test your sample, it seems work well in my local test.... – zangw Mar 06 '16 at 13:17
  • Also notice, you query the phone, and set the same phone number in the object to save? – zangw Mar 06 '16 at 13:20
  • @zangw: it prints the message in callback and inserts data in db? – MarcoS Mar 06 '16 at 13:20
  • Yes, here are the outputs `trace 3331234567 http://www.example.com/ updated trace updated: { dateOfLastSync: Sun Mar 06 2016 21:16:56 GMT+0800 (CST), __v: 0, title: 'title 1', phone: '3331234567', link: 'http://www.example.com/', _id: 56dc2d96de44a1166e31005e }` – zangw Mar 06 '16 at 13:25
  • here it does nothing... :-( should I restart MongoDB??? – MarcoS Mar 06 '16 at 13:26
  • nothing changed after restart... :-((( – MarcoS Mar 06 '16 at 13:27
  • I'm assuming you might be using express. Do you see any error on console? Why it's terminating? – Saleem Mar 06 '16 at 14:43
  • I'm using express, but for this test I did extrapolate the pure JS code, so, no express, just running `node test.js`, so any error should be output to console, I suppose... I don't know why it's terminating! Is there some debug I can try? – MarcoS Mar 06 '16 at 16:13
  • @zangw: I'm just testing this strange behavior, I can't even **insert** one document, let alone update (for the moment)... – MarcoS Mar 06 '16 at 16:15

1 Answers1

1

Sorry, everybody!
I just found my silly mistake: simply I was not including "db.js", in which I do the mongoose.connect(...) stuff.

I post this as an answer so fewer people will loose time with this thing...
(Although I don't know if the fact that I got no error is good... :-)

Sooooorry again...

MarcoS
  • 17,323
  • 24
  • 96
  • 174