0

How to resolve "mpromise (mongoose's default promise library) is deprecated" from a Typescript Application.

I'm getting the following error:

Left-hand side of assignment expression cannot be a constant or a read-only property.

I am using a MEAN stack with Angular 2 and would like to have mongoose use the bluebird promises library.

I am getting an error when I attempt to follow these instructions on Stack Overflow and Mongo

To be honest, I'm unsure if my issue is just lack of knowledge of Typescript or if I am doing something else wrong.

"use strict";

import * as mongoose from 'mongoose';
var dbConst = require('../constants/db.json');
var bluebird = require("bluebird");

export class DBConfig {
    static init():void {
      const URL = (process.env.NODE_ENV === 'production') ? process.env.MONGOHQ_URL
                                                          : dbConst.localhost;

      mongoose.Promise = bluebird;     // <-- THIS IS WHERE ERROR OCCURS
      mongoose.connect(URL);
      mongoose.connection.on('error', console.error.bind(console, 'An error ocurred with the DB connection: '));
    }
};
Community
  • 1
  • 1
David Cruwys
  • 6,262
  • 12
  • 45
  • 91

1 Answers1

0

I believe the following should work:

import * as mongoose from "mongoose";
import * as bluebird from "bluebird";

//either this
(<any>mongoose).Promise = bluebird;

//OR pass it in as an option
const connection = mongoose.createConnection("mongodb://localhost:27017", { 
    promiseLibrary: bluebird
});
spyter
  • 727
  • 1
  • 9
  • 25