0

I am developing a NodeJS application. I had a problem saving an object to the database; the code in my controller was:

console.log("Before save: " + transaction);
transaction = await transaction.save();
console.log("After save");

In the terminal running the Node application, I saw:

Before save: {
  _id: ...,
  created: ...,
  ...
}

and nothing more, while the browser page looks like it's loading.

I tried wrapping it in a try-catch block:

try {
  transaction = await transaction.save();
} catch (err) {
  console.log(err);
}

and I get the same output in the terminal. I suppose that since I don't see the error in the console without the try-catch, I don't see it with it either.

One problem was that the MongoDB collection for that object did not exist (and I asked about it here). But I still have another error.

How can I enable MongoDB to show those errors (missing collection, and the error I am still looking for) in the terminal running NodeJS while I'm in development?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
  • You can use `try-catch`; place your code which might throw an error in the try block. See [try-catch with NodeJS](https://nodejs.org/en/knowledge/errors/what-is-try-catch/) and [MongoDB docs with try-catch examples](https://mongodb.github.io/node-mongodb-native/3.5/reference/ecmascriptnext/crud/). – prasad_ Mar 31 '20 at 14:17
  • @prasad_ I did try that and forgot to put it in the question. I added it now. – miguelmorin Mar 31 '20 at 14:24
  • _"I found one problem in that the MongoDB collection for that object did not exist."_ you mean the collection did not exist in the database and you are trying to query it? Its not an error. – prasad_ Mar 31 '20 at 14:28
  • @prasad_ Sorry for the confusion, I made it more explicit now. It was the collection that did not exist. I still have one error. – miguelmorin Mar 31 '20 at 14:33
  • About the missing collection, the code in your application must check its existence before any operations on the collection. If you add a document to an non-existing collection, the server will create the collection and inserts the document into it. But, that all depends upon your application's logic or functionality. – prasad_ Mar 31 '20 at 14:35
  • I found [this answer](https://stackoverflow.com/questions/9457368/inserting-data-to-mongodb-no-error-no-insert/9458077#9458077) that mentions MongoDB uses "fire and forget" and does not return errors, unless we pass "safe: true". I tried several syntaxes and failed. Do you know the syntax for NodeJS? – miguelmorin Mar 31 '20 at 14:50
  • 1
    @miguelmorin For "safe: true" you'll have to pass that in an object as second param to Schema definition. src: https://mongoosejs.com/docs/2.7.x/docs/schema-options.html – Akash Jobanputra Mar 31 '20 at 15:01
  • @AkashJobanputra I learned that `safe: true` is the default, and I also tried that and got the same error. – miguelmorin Mar 31 '20 at 15:17
  • @miguelmorin can you share a short reproducible snippet? – Akash Jobanputra Mar 31 '20 at 18:08
  • I found the problem: I had omitted a `next();` in a pre-save hook. So I was not getting an error because there was no error, only a break in the flow of calls. Do either of you want to write an answer? – miguelmorin Mar 31 '20 at 18:49
  • @miguelmorin You figured it out yourself, you should add the answer. ;) – Akash Jobanputra Mar 31 '20 at 19:10

1 Answers1

0

I found the problem: I had omitted a next(); in a pre-save hook:

SomeSchema.pre('save', function(next) {
  // any logic you require
  if (this.condition) {
    // ...
  }
  next();  // <- this is required, else Mongo won't save nor return
});

So I was not getting an error because there was no error, only a break in the flow of calls.

miguelmorin
  • 5,025
  • 4
  • 29
  • 64