0

I'm using mongodb in node.js with Gridfs and I encounter TypeError: Promise is not a constructor on this line. I've traced this back a little bit, and I can see that on this line, topology.s.promiseLibrary is undefined. I don't know what I could have done to cause this. It was my understanding that mongodb would just use the built-in ES6 Promise even if I don't hook up bluebird or something like that.

Curiously, topology.s.options.promiseLibrary is defined. I'm not sure if there's something I've done wrong to configure it. I haven't changed my code (I've reverted back to code that was working before). My node version is v9.2.0. My Mongodb is 3.10.0. My gridfs-stream is 1.1.1.

If I comment out this line then everything works fine.

const Promise = topology.s.promiseLibrary;

Is this a bug? What might be the cause? It seems like there might be an external cause, but I'm not sure where to continue my search. How is topology.s.promiseLibrary supposed to be getting set? Why isn't it defined?

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • I think you mean your MongoDB "node driver" version is `3.0.10` which is the latest "stable" public release. If you're headed down the track where you think you've found a bug ( and that looks like where you're going ) then stop and listen as I tell you that particular part of code is stable and unchanged for over a year now. The problem is not in the driver but in your code. Show the "minimal" code that you can which you believe reproduces the error, and then someone can happily look at it. – Neil Lunn Jun 12 '18 at 13:24
  • Also, please run on an LTS Node. 9.x is discontinued. Current stable LTS is 8.x and 10.x has been out for a while and approaching LTS, therefore acceptable for testing. But you should not be running on 9.x as it is a discontinued development series release. – Neil Lunn Jun 12 '18 at 13:26
  • OK, thanks. I'll change versions and try to prepare a repro. That will take a little bit of time. Meanwhile, can you describe the flow by which there is supposed to be a valid promiseLibrary sitting in `topology.s.options.promiseLibrary`. What could I possibly have done to make it undefined? I fear that the long road of coming up with a repro and then bisecting to discover what went wrong will be time consuming and I was hoping for a little information to help me guess what kinds of things I might have done wrong. – Wyck Jun 12 '18 at 13:44
  • [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) you doing exactly what you need to do in order to reproduce is exactly the point. 9/10 times you spot what you did wrong yourself, and it saves a lot of back an forth trying to get you to show the right part of the code. Show a listing that reproduces if you believe there is a problem. – Neil Lunn Jun 12 '18 at 13:54
  • I'm sorry I was not able to post a complete example. There are just too many required parts. So far I can only get it to happen when using a browser to post a file to a node.js/express server received through busboy to a gridfs-stream set up with mongoose/mongodb when using bluebird as mongoose's promise library. But that's way too much to post in any kind of complete form. There are entire tutorial websites devoted to setting up that much framework. Fortunately dropping bluebird in favour of ES6 promises will be an acceptable (although mysterious) workaround. – Wyck Jun 12 '18 at 20:24

2 Answers2

0

I stopped using bluebird for my promise library in favour of using the built-in ES6 Promise library, and now the exception has gone away. I have NO IDEA why I can't use bluebird. It's been working forever, then suddenly this week: nope.

Changed

mongoose.Promise = require('bluebird');

to

mongoose.Promise = Promise;
Wyck
  • 10,311
  • 6
  • 39
  • 60
0

Somehow my mongodb was actually set to 3.1.0-beta4 and that seems to have caused this problem.

I pinned mongodb to 2.2.34 and everything is fine.

Wyck
  • 10,311
  • 6
  • 39
  • 60