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: '));
}
};
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
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.